我有以下场景:我想多次在后台线程上调用一个方法,但是返回的值来自一个委托方法,我认为它是在主线程上调用的。我如何在后台线程上处理这个?
NSOperationQueue *operationQueue = [NSOperationQueue new];
for (int i = 0 ; i < 100; i++) {
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
[self.routingService calculateRoute:self.routeSettings];
}];
[self.operationQueue addOperation:blockOperation];
}
//Delegate method
- (void)routingService:(SKRoutingService *)routingService didFinishRouteCalculationWithInfo:(SKRouteInformation *)routeInformation {
//Here I want to process routeInformation
}
请帮帮我。 :)
答案 0 :(得分:1)
您可以在委派方法
中执行此操作dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//add your processing code here
});
答案 1 :(得分:0)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//add your background processing code here
dispatch_async(dispatch_get_main_queue(), ^{
//add processing on main thread
});