NSOperation and NSOperationQueue working thread vs main thread

后端 未结 6 2007
心在旅途
心在旅途 2020-11-28 01:00

I have to carry out a series of download and database write operations in my app. I am using the NSOperation and NSOperationQueue for the same.

6条回答
  •  -上瘾入骨i
    2020-11-28 01:48

    The summary from the docs is operations are always executed on a separate thread (post iOS 4 implies GCD underlying operation queues).

    It's trivial to check that it is indeed running on a non-main thread:

    NSLog(@"main thread? %@", [NSThread isMainThread] ? @"YES" : @"NO");
    

    When running in a thread it's trivial to use GCD/libdispatch to run something on the main thread, whether core data, user interface or other code required to run on the main thread:

    dispatch_async(dispatch_get_main_queue(), ^{
        // this is now running on the main thread
    });
    

提交回复
热议问题