gpt4 book ai didi

ios - 如何接收异步任务完成的通知

转载 作者:行者123 更新时间:2023-12-01 17:59:09 25 4
gpt4 key购买 nike

我在 prepareForSegue 回调中有 NSOperationQueue 和 3 NSInvocationOperation ,我需要在完成所有异步任务后移动到另一个 View Controller 。
只有在移动到另一个屏幕之后,我才能收到关于完成所有异步任务的通知?

我试过这个没有成功:

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 中保存图像

最佳答案

您可以使用 dispatch_group_async()如果你打算使用 GCD 来执行你的异步任务,函数:Grand Central Dispatch (GCD) Reference

dispatch_group_t group = dispatch_group_create();

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
//Code here is executed asynchronously
});

dispatch_group_notify(group, dispatch_get_main_queue(),
^{
//Do something when async has completed
//Note: You are not required to use the main
//queue if you aren't performing any UI work.
});

dispatch_release(group);

关于ios - 如何接收异步任务完成的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12790525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com