I want to know how to call my function
on the main thread.
How do I make sure my function
is called on the main thread?
(this follows a previous question of mine).
This question is related to
ios
xcode
multithreading
multitasking
shared-resource
i think this is cool, even tho in general its good form to leave the caller of a method responsible for ensuring its called on the right thread.
if (![[NSThread currentThread] isMainThread]) {
[self performSelector:_cmd onThread:[NSThread mainThread] withObject:someObject waitUntilDone:NO];
return;
}
This will do it:
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
//Your code goes in here
NSLog(@"Main Thread Code");
}];
Hope this helps!
When you're using iOS >= 4
dispatch_async(dispatch_get_main_queue(), ^{
//Your main thread code goes in here
NSLog(@"Im on the main thread");
});
Source: Stackoverflow.com