Check whether or not the current thread is the main thread
Have a look at the NSThread
API documentation.
There are methods like
- (BOOL)isMainThread
+ (BOOL)isMainThread
and + (NSThread *)mainThread
In Swift3
if Thread.isMainThread {
print("Main Thread")
}
If you want a method to be executed on the main thread, you can:
- (void)someMethod
{
dispatch_block_t block = ^{
// Code for the method goes here
};
if ([NSThread isMainThread])
{
block();
}
else
{
dispatch_async(dispatch_get_main_queue(), block);
}
}