Check whether or not the current thread is the main thread - Printable Version +- 0Day Forums (https://zeroday.vip) +-- Forum: Coding (https://zeroday.vip/Forum-Coding) +--- Forum: Objective-C (https://zeroday.vip/Forum-Objective-C) +--- Thread: Check whether or not the current thread is the main thread (/Thread-Check-whether-or-not-the-current-thread-is-the-main-thread) Pages:
1
2
|
Check whether or not the current thread is the main thread - acknowledgment715152 - 07-21-2023 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!"); } } RE: Check whether or not the current thread is the main thread - sarahucxajavab - 07-21-2023 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); } } RE: Check whether or not the current thread is the main thread - rashidki - 07-21-2023 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. RE: Check whether or not the current thread is the main thread - Projolexfjjaqm - 07-21-2023 **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 RE: Check whether or not the current thread is the main thread - naumachia404 - 07-21-2023 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"); RE: Check whether or not the current thread is the main thread - colobus41 - 07-21-2023 For Monotouch / Xamarin iOS you can perform the check in this way: if (NSThread.Current.IsMainThread) { DoSomething(); } else { BeginInvokeOnMainThread(() => DoSomething()); } RE: Check whether or not the current thread is the main thread - obedientialness469574 - 07-21-2023 Swift Version - - - - if (NSThread.isMainThread()) { print("Main Thread") } RE: Check whether or not the current thread is the main thread - hammerwort506403 - 07-21-2023 **In Swift3** if Thread.isMainThread { print("Main Thread") } RE: Check whether or not the current thread is the main thread - understrike193902 - 07-21-2023 > 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] RE: Check whether or not the current thread is the main thread - Mrnonschematizedsev - 07-21-2023 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 } |