在 for 循环中 findObjectsInBackgroundWithBlock 完成后执行操作 [英] Perform action after findObjectsInBackgroundWithBlock completed in for loop

查看:23
本文介绍了在 for 循环中 findObjectsInBackgroundWithBlock 完成后执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有这样的结构:

I have such construction in my code:

for (METMeetingEntity *e in self.meetingList) {
            PFQuery *query = [PFUser query];        
            //some query constraints, depending on METMeetingEntity
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
            {
                // some query actions


                NSArray *sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];
                self.meetingList = [NSMutableArray arrayWithArray:[self.meetingList sortedArrayUsingDescriptors:sortDescriptors]];
                self.meetingList = [self dateCleanup:self.meetingList];
            }];

如何执行操作 - 在所有 findObjectsInBackground 完成后重新加载我的表视图

How can I perform action - reload my table view after all the findObjectsInBackground are completed

推荐答案

一种可能的解决方案是保持计数.当数字与原始计数匹配时,您就知道完成了.

One possible solution would be to keep a count. When the number matches the original count, you know you are done.

NSUInteger count = self.meetingList.count;
for (METMeetingEntity *e in self.meetingList) {
    PFQuery *query = [PFUser query];        
    //some query constraints, depending on METMeetingEntity
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        // some query actions
        NSArray *sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];
        self.meetingList = [NSMutableArray arrayWithArray:[self.meetingList sortedArrayUsingDescriptors:sortDescriptors]];
        self.meetingList = [self dateCleanup:self.meetingList];

        count--;
        if (count == 0) {
            dispatch_async(dispatch_get_main_queue(), ^{
                // reload table or whatever needs to be done
            });
        }
    }];
}

这篇关于在 for 循环中 findObjectsInBackgroundWithBlock 完成后执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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