gpt4 book ai didi

ios - dispatch_queue 和返回数据

转载 作者:行者123 更新时间:2023-11-28 22:41:59 24 4
gpt4 key购买 nike

我正在尝试编写返回 NSArray 的方法。我的 NSMutableArray (friendUsers) 添加了正确的对象,但在 dispatch_async 之外,数组是空的。我尝试在主队列中添加用户(如图所示),但数组为空。有任何想法吗 ?感谢您的帮助。

- (NSArray *)checkUsersInGroup {

NSMutableArray *friendUsers = [[NSMutableArray alloc] init];

dispatch_queue_t checkUSers = dispatch_queue_create("CheckUsers", NULL);
dispatch_async(checkUSers, ^{

NSArray *totalUsers = [VVDataRead lecturaDades];
NSArray *usersToSearch = [_grup objectForKey:@"groupFriends"];

for (NSString *tempUserId in usersToSearch){
for (NSDictionary *user in totalUsers){
NSString *id = [user objectForKey:@"id"];
if ([tempUserId isEqualToString:id])
dispatch_async(dispatch_get_main_queue(), ^{
[friendUsers addObject:user];
});
}
}

});
NSLog(@"people:%@",friendUsers);
return [friendUsers copy];
}

最佳答案

你可以使用积木,在这种情况下它可以让你的生活更轻松。

- (void)checkUsersInGroupWithCompleteBlock:(void(^)(NSMutableArray * resultArray))completeBlock {

NSMutableArray *friendUsers = [[NSMutableArray alloc] init];

dispatch_queue_t checkUSers = dispatch_queue_create("CheckUsers", NULL);
dispatch_async(checkUSers, ^{

NSArray *totalUsers = [VVDataRead lecturaDades];
NSArray *usersToSearch = [_grup objectForKey:@"groupFriends"];

for (NSString *tempUserId in usersToSearch){
for (NSDictionary *user in totalUsers){
NSString *id = [user objectForKey:@"id"];
if ([tempUserId isEqualToString:id])
dispatch_async(dispatch_get_main_queue(), ^{
[friendUsers addObject:user];
});
}
}

// call the complete block with the result when you finished
if (completeBlock) completeBlock(friendUsers);
});
}

...下面是调用方法的方法:

- (void)anyMethod {

// ... do whetever you want here before

[self checkUsersInGroupWithCompleteBlock:^(NSMutableArray *resultArray) {
NSLog(@"%@", resultArray);
}];

// ... or after

}

已编辑:

注意:这是另一种可能的解决方案,但在您的情况下,它只是暂停主线程(这绝对不好),因此您使用此解决方案除了痛苦之外不会有任何收获在主线程上,但如果您在两个后台线程上,此解决方案可以提供一个很好的线程间同步示例。

- (NSArray *)checkUsersInGroup {

NSMutableArray *friendUsers = [[NSMutableArray alloc] init];

// our semaphore is here
dispatch_semaphore_t _semaphore = dispatch_semaphore_create(0);

dispatch_queue_t checkUSers = dispatch_queue_create("CheckUsers", NULL);
dispatch_async(checkUSers, ^{

NSArray *totalUsers = [VVDataRead lecturaDades];
NSArray *usersToSearch = [_grup objectForKey:@"groupFriends"];

for (NSString *tempUserId in usersToSearch){
for (NSDictionary *user in totalUsers){
NSString *id = [user objectForKey:@"id"];
if ([tempUserId isEqualToString:id])
dispatch_async(dispatch_get_main_queue(), ^{
[friendUsers addObject:user];
});
}
}
// the process finished
dispatch_semaphore_signal(_semaphore);

});

// ... we are wainitng for the semaphore's signal
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(_semaphore);

NSLog(@"people:%@",friendUsers);
return [friendUsers copy];

}

关于ios - dispatch_queue 和返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14276704/

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