我可以使用GCD作为后台线程并使用performSelector恢复回主线程吗?

时间:2016-04-13 12:19:46

标签: ios objective-c

让我们有一个场景如下,

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Doing some DB operation or Server call

   // After getting result i am making UI update in main thread
   [self performSelectorOnMainThread:@selector(myMethod) withObject:nil waitUntilDone:YES];
});

通过类似

的方法在后台调用执行选择器
- (void)callingWSMethod {
    [self performSelectorInBackground:@selector(myMethod) withObject:nil];
}

- (void)myMethod {
    // Doing some DB operation or Server call

    dispatch_async(dispatch_get_main_queue(), ^{
         // After getting result i am making UI update in main thread
    });
}
  1. 我可以将performSelector和GCD结合使用吗?

  2. 以上两种制作后台线程和主线程调用的类型都会得到相同的结果吗? 感谢。

0 个答案:

没有答案