0

I'm trying to display a UIActivityIndicatorView while background processing takes place. The below simplified code works when I try it in the simulator(the alert is displayed)..but when I download it to my phone from Xcode, the background thread does not seem to get called at all. (the alert never gets displayed)
Any ideas?

 -(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];    
}
1
  • Also, for multithreading it's really worth looking into NSOperation and NSOperationQueue. It really makes your life a whole lot easier
    – nduplessis
    Mar 4, 2009 at 21:04

3 Answers 3

1

Have you you tried cleaning your build? I just ran your code on my device and simulator and it works as expected in both cases

1

Use NSOperation instead of raw thread manipulation. It abstracts all sorts of stuff for you (priority, autoreleasepools etc...). ? You can simply add some kind of delegate to your NSOperation subclass to get a callback when you need.

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.

2
  • you should update your question with this information, rather than putting it in an answer, so others won't try to answer. Mar 4, 2009 at 22:23
  • as Kristopher noted, you can update the original question and delete this answer. Mar 25, 2009 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.