gpt4 book ai didi

ios - dispatch_group_notify 不等待一个 dispatch_group_async

转载 作者:可可西里 更新时间:2023-11-01 03:06:54 27 4
gpt4 key购买 nike

两个并发的后台任务需要修补两个单独的数组,需要合并到一个dispatch_group_notify block 中。问题是,第一个 block 超出了,但是dispatch_group_notify超出了,没有等待第二个后台任务的执行。它们之间的唯一区别是第一个进行本地搜索,第二个对 Web 服务进行远程调用。知道为什么跳过第二个吗?

编辑:我也尝试了 https://stackoverflow.com/a/19580584/859742 中提到的方法使用 dispatch_barrier_async 但仍然相同。

 dispatch_group_t taskGroup = dispatch_group_create(); 
dispatch_queue_t mainQueue = dispatch_get_main_queue();

__block NSArray *localAddresses;
__block NSArray *remoteAddresses;

//Get local array in the background
dispatch_group_async(taskGroup, mainQueue, ^{
//localAddresses is set
});

//get remote array from server
dispatch_group_async(taskGroup, mainQueue, ^{
[[MDAddressManager instance] searchForPlacesContainingText:query
location:alocation
completion:^(NSArray* addresses, MDError *error){
//remoteAddresses is set
});


//Merge two arrays
dispatch_group_notify(taskGroup, mainQueue, ^{
//remoteAddresses and local addresses are merged
});

远程搜索方法是这样的

- (void)searchForPlacesContainingText:(NSString *)searchText
location:(CLLocation *)alocation
completion:(MDAddressManagerBlock)completionBlock
{
NSDictionary *parameters = [[NSMutableDictionary alloc] init];

[parameters setValue:searchText forKey:@"input"];

[[MDHTTPClient sharedHTTPClient] getPath:@"v1/remotePlaces.json"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id dict) {

if ([MDHTTPClient isResponseValid:dict])
{
completionBlock(returnArray, nil);
}
else
{
completionBlock(nil, nil);
}

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
EDLog(@"%@", error);
completionBlock(nil, [MDError errorAFNetworking:error]);
}];
}

最佳答案

这是因为您的 getPath 方法是异步运行的。您需要它在完成 block 运行之前不要离开组。因此,与其执行 dispatch_group_async,不如手动执行 dispatch_group_enterdispatch_group_leave

您可以更改您的代码:

dispatch_group_async(taskGroup, mainQueue, ^{
[[MDAddressManager instance] searchForPlacesContainingText:query
location:alocation
completion:^(NSArray* addresses, MDError *error){
//remoteAddresses is set
}];
});

收件人:

dispatch_group_enter(taskGroup);

[[MDAddressManager instance] searchForPlacesContainingText:query
location:alocation
completion:^(NSArray* addresses, MDError *error){
//remoteAddresses is set
dispatch_group_leave(taskGroup);
});

这将确保您在调用完成 block 之前不会离开组。


或者,您可以更改 searchForPlacesContainingText 以使用 dispatch_group_t 参数:

- (void)searchForPlacesContainingText:(NSString *)searchText
location:(CLLocation *)alocation
group:(dispatch_group_t)group
completion:(MDAddressManagerBlock)completionBlock
{
dispatch_group_enter(group);

NSDictionary *parameters = [[NSMutableDictionary alloc] init];

[parameters setValue:searchText forKey:@"input"];

[[MDHTTPClient sharedHTTPClient] getPath:@"v1/remotePlaces.json"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id dict) {

if ([MDHTTPClient isResponseValid:dict])
{
completionBlock(returnArray, nil);
}
else
{
completionBlock(nil, nil);
}

dispatch_group_leave(group);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
EDLog(@"%@", error);
completionBlock(nil, [MDError errorAFNetworking:error]);

dispatch_group_leave(group);
}];

}

并更改您的调用,使其不执行 dispatch_group_async,而只是传递 taskGroup 参数:

[[MDAddressManager instance] searchForPlacesContainingText:query
location:alocation
group:taskGroup
completion:^(NSArray* addresses, MDError *error) {
//remoteAddresses is set
});

关于ios - dispatch_group_notify 不等待一个 dispatch_group_async,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23828798/

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