MoPhO/iPhoneDevelopment
Appearance
Code snippets
Update view from a different thread (not the main thread)
If you have a function that runs on a different thread and you want to update something in a View, you will need to add a NSAutoreleasePool to that thread (or at least the function) and then you need to call the performSelectorOnMainThread method on the view or object you are updating.
For example, assuming that data is a pointer to a UILabel in the view that we want to update in the callback:
// Function not running in the Main Thread
void callback( void * data )
{
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
UILabel * me = (UILabel *)data;
[me performSelectorOnMainThread:@selector(setText:) withObject:@"New text" waitUntilDone:YES];
[autoreleasepool release];
}
Instead of a UILabel you can pass any other widget.