gpt4 book ai didi

ios - NSOperationQueue 如何等待两个异步操作?

转载 作者:可可西里 更新时间:2023-11-01 06:17:44 25 4
gpt4 key购买 nike

如何让 NSOperationQueue(或其他任何东西)等待两个带有回调的异步网络调用?流程需要看起来像这样

Block Begins {
Network call with call back/block begins {
first network call is done
}
}
Second Block Begins {
Network call with call back/block begins {
second network call is done
}
}

Only run this block once the NETWORK CALLS are done {
blah
}

这是我目前所拥有的。

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
__block NSString *var;


[queue addOperation:[NSBlockOperation blockOperationWithBlock:^{
[AsyncReq get:^{
code
} onError:^(NSError *error) {
code
}];
}]];

[queue addOperation:[NSBlockOperation blockOperationWithBlock:^{
[AsyncReq get:^{
code
} onError:^(NSError *error) {
code
}];
}]];
[queue waitUntilAllOperationsAreFinished];
//do something with both of the responses

最佳答案

你必须使用NSOperation Queue吗?以下是使用调度组执行此操作的方法:

dispatch_group_t group = dispatch_group_create();

dispatch_group_enter(group);
[AsyncReq get:^{
code
dispatch_group_leave(group);
} onError:^(NSError *error) {
code
dispatch_group_leave(group);
}];


dispatch_group_enter(group);
[AsyncReq get:^{
code
dispatch_group_leave(group);
} onError:^(NSError *error) {
code
dispatch_group_leave(group);
}];

dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"Both operations completed!")
});

关于ios - NSOperationQueue 如何等待两个异步操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27259570/

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