From CCRMA Wiki
Revision as of 12:33, 12 November 2009 by Jorgeh (Talk | contribs) (Code snippets)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Code snippets

Pleas add any snippet of code that you think might be helpful to others.

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.