Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 738 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check whether or not the current thread is the main thread

#1
Is there any way to check whether or not the current thread is the main thread in Objective-C?

I want to do something like this.

- (void)someMethod
{
if (IS_THIS_MAIN_THREAD?) {
NSLog(@"ok. this is main thread.");
} else {
NSLog(@"don't call this method from other thread!");
}
}
Reply

#2
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);
}
}
Reply

#3
If you want to know whether or not you're on the main thread, you can simply use the debugger. Set a breakpoint at the line you're interested in, and when your program reaches it, call this:

`(lldb) thread info`

This will display information about the thread you're on:


```(lldb) thread info
thread #1: tid = 0xe8ad0, 0x00000001083515a0 MyApp`MyApp.ViewController.sliderMoved (sender=0x00007fd221486340, self=0x00007fd22161c1a0)(ObjectiveC.UISlider) -> () + 112 at ViewController.swift:20, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
```

If the value for `queue` is `com.apple.main-thread`, then you're on the main thread.
Reply

#4
**UPDATE:** seems that is not correct solution, according to queue.h header as mentioned @demosten

The first thought was brought to me, when I was needed this functionality was the line:

dispatch_get_main_queue() == dispatch_get_current_queue();

And had looked to the accepted solution:

[NSThread isMainThread];

mine solution 2.5 times faster.

PS And yes, I'd checked, it works for all threads
Reply

#5
Two ways. From @rano's answer,

[[NSThread currentThread] isMainThread] ? NSLog(@"MAIN THREAD") : NSLog(@"NOT MAIN THREAD");

Also,

[[NSThread mainThread] isEqual:[NSThread currentThread]] ? NSLog(@"MAIN THREAD") : NSLog(@"NOT MAIN THREAD");
Reply

#6
For Monotouch / Xamarin iOS you can perform the check in this way:

if (NSThread.Current.IsMainThread)
{
DoSomething();
}
else
{
BeginInvokeOnMainThread(() => DoSomething());
}
Reply

#7
Swift Version

- - - -

if (NSThread.isMainThread()) {
print("Main Thread")
}
Reply

#8
**In Swift3**

if Thread.isMainThread {
print("Main Thread")
}

Reply

#9
> let isOnMainQueue =
> (dispatch_queue_get_label(dispatch_get_main_queue()) ==
> dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL))

check this answer from

[To see links please register here]

Reply

#10
The following pattern will assure a method is executed on the main thread:

- (void)yourMethod {
// make sure this runs on the main thread
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:_cmd/*@selector(yourMethod)*/
withObject:nil
waitUntilDone:YES];
return;
}
// put your code for yourMethod here
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through