In my app I want to make un update of my database when I press a button. Because it takes some time for this job and not to freeze the hole app I decided to use NSThread.
This process can be started from different views, so I have placed the update code in my App Delegate. When pressing the update method I have this code:
MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:delegate withObject:nil];
In my delegate:
- (void)startTheBackgroundJob {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//do some stuff here
self.percentageCompleted = (i * 100) / totalChars;
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshObjectsForUpdate" object:self];
//some other stuff
[pool release];
Basically inside my thread I want to update different progressViews with current progress. So I used NSNotificationCenter to send a message after each step of my thread.
In a viewController I am catching this message and I am doing:
MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
double prc = delegate.percentageCompleted;
UILabel *l = (UILabel *)[self.view viewWithTag:444];
l.text = [NSString stringWithFormat:@"%f", prc];
But it refreshes my label only at the end of the thread.
What is the problem here? Can I used another approach for this?
Thanks.