1

I would like to detect if a method in objective c is called and executing on NSThread that is running on foreground or background.

Thanks

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • Place a breakpoint inside said method, then when the app pauses on that method you will be able to see which thread it is being run on. – Nick May 27 '14 at 10:42

2 Answers2

3

You can check whether the current thread is the main one with:

+ (BOOL)[NSThread isMainThread]
Antonio E.
  • 4,381
  • 2
  • 25
  • 35
0
(void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.

        //this thread called method in background thread
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
                                                 (unsigned long)NULL), ^(void) {
            [self callBackMethod];
        });

        //this method call work with main thread
        [self callBackMethod];
    }
    -(void)callBackMethod
    {
        if ([NSThread isMainThread]) {
            NSLog(@"main thread....");
        }else
        {
            NSLog(@"background thread..");
        }
    }
Ashwinkumar Mangrulkar
  • 2,956
  • 3
  • 15
  • 17