0

我正在尝试在进行后台处理时显示 UIActivityIndi​​catorView 。下面的简化代码在我在模拟器中尝试时有效(显示警报)..但是当我从 Xcode 将它下载到我的手机时,后台线程似乎根本没有被调用。(警报永远不会显示)有
什么想法吗?

 -(void)viewDidLoad {   
    [self performSelectorInBackground:@selector(runInAnotherThread) withObject:nil];

}

-(void) runInAnotherThread {
    NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
  int i;
    for(i=0;i < 1000 ;i ++){
        NSLog(@"INDEX = %d", i);
    }

    [self performSelectorOnMainThread : @ selector(backToMainThread ) withObject:nil waitUntilDone:NO]; 
    [ pool release ];
}

-(void) backToMainThread {

    UIAlertView *completeAlert = [[UIAlertView alloc] 
                     initWithTitle:@"Back to main "
                message: @"Success" 
                delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil];
    [completeAlert show];
    [completeAlert release];    
}
4

3 回答 3

1

你试过清理你的构建吗?我刚刚在我的设备和模拟器上运行了您的代码,它在两种情况下都按预期工作

于 2009-03-04T21:02:19.407 回答
1

使用 NSOperation 而不是原始线程操作。它为您抽象了各种东西(优先级、自动释放池等......)。? 您可以简单地向您的 NSOperation 子类添加某种委托,以便在需要时获得回调。

于 2009-03-04T21:18:17.950 回答
0

Thanks for replying so quickly!

It turned out that the issue was not in this code fragment at all. I was executing this code dependent on a value in the keychain. While my simulator's keychain has that value, my test iphone did not have this value.

Feel so silly for troubling all of you. But following up on the reply from nduplessis helped me narrow down the issue.

于 2009-03-04T21:37:34.860 回答