我在 prepareForSegue 回调中有 NSOperationQueue 和 3 NSInvocationOperation ,我需要在完成所有异步任务后移动到另一个视图控制器。只有在移动到另一个屏幕之后,我才能收到关于完成所有异步任务的通知?
我试过这个没有成功:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
});
编辑:
似乎我不明白一些事情:
-(void)initPurchase{
[operationQueue cancelAllOperations];
NSInvocationOperation *downloadImageOperation = [[NSInvocationOperation alloc] initWithTarget:[BSImageDownloader getInstance] selector:@selector(downloadImageSync:) object:@"http://.........jpg"];
NSInvocationOperation *createImageOperation = [[NSInvocationOperation alloc] initWithTarget:[BSImageCreator getInstance] selector:@selector(createImage:) object:@"dsadsadsa"];
NSInvocationOperation *saveImageOperation = [[NSInvocationOperation alloc] initWithTarget:[BSImageSaver getInstance] selector:@selector(saveImageAsPng:) object:[BSSharedObject getInstance].createdImage];
[createImageOperation addDependency:downloadImageOperation];
[saveImageOperation addDependency:createImageOperation];
[operationQueue addOperation:downloadImageOperation];
[operationQueue addOperation:createImageOperation];
[operationQueue addOperation:saveImageOperation];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
[[BSPopupManager getInstance]showWaitingPopup];
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
Purchase *purchase = [[Purchase alloc] init];
[purchase initPurchase];
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
[[BSPopupManager getInstance] closeWaitingPopup];
BSPurchaseViewController *purchaseViewController = [segue destinationViewController];
purchaseViewController.pngImage = [BSSharedObject getInstance].createdImage;
NSLog(@"2");
});
dispatch_release(group);
}
}
我仍然得到 NSLog(@"2"); 早于我在第三个 NSInvocationOperation 中保存图像