views:

295

answers:

3

Hi
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  A: 

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

nduplessis
+1  A: 

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.

François P.
A: 

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.

Naren
you should update your question with this information, rather than putting it in an answer, so others won't try to answer.
Kristopher Johnson
done. sorry newbie mistake
as Kristopher noted, you can update the original question _and_ delete this answer.
Can Berk Güder