dispatch_queue并返回数据 [英] dispatch_queue and return data

查看:140
本文介绍了dispatch_queue并返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to write this method that returns an NSArray. My NSMutableArray (friendUsers) adds the objects right, but outside the dispatch_async the array is empty. I try to add the users in the main queue ( as ashowed) but the array is empty to. Any ideas ? Thanks for all your help.

- (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];
}

推荐答案

您可以使用积木,在这种情况下,可以使您的生活更轻松.

you can use blocks, it can make your life easier in this case.

- (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);
    });
}

...这是调用方法的方式:

...and here is how you can call the method:

- (void)anyMethod {

    // ... do whetever you want here before

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

    // ... or after

}


已编辑

注意:这是另一种可能的解决方案,但是在您的情况下,它只是挂起了主线程(这肯定是),因此,使用该解决方案您将一无所获在主线程上,但是如果您在两个后台线程上,则此解决方案可以提供一个很好的示例,说明线程之间的同步.

NOTE: here is another possible solution, but in your case it just suspends the main thread (which is definitely bad), so you won't gain anything with this solution but pain on the main thread, but if you are on two background threads, this solution can give a very nice example of synchronisation between the threads.

- (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];

}

这篇关于dispatch_queue并返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆