检测是否在后台/前台调用方法

时间:2014-05-27 10:38:12

标签: ios background nsthread

我想检测目标c中的方法是否在前台或后台运行的NSThread上调用并执行。

由于

2 个答案:

答案 0 :(得分:3)

您可以检查当前主题是否为主要主题:

+ (BOOL)[NSThread isMainThread]

答案 1 :(得分: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..");
        }
    }