我需要执行异步函数执行,因为它阻塞了主线程,因此 UI 不可用。
看了stackoverflow中的问题后,我知道有三种方法可以做异步函数。
一个例子:
[NSThread detachNewThreadSelector:@selector(showSpinner:) toTarget:self withObject:self.view];
// or
[self performSelectorInBackground:@selector(showSpinner:) withObject:self.view];
// or
NSInvocationOperation *invOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(showSpinner:) object:self.view];
NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
[opQueue addOperation:invOperation];
// or
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
[self showSpinner:self.view];
});
});
performSelectorInBackground
我的问题是如何detachNewThreadSelector
返回主线程?你怎么知道他们已经完成了?