r循环中完成findObjectsInBackgroundWit

r循环中完成findObjectsInBackgroundWit

本文介绍了在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后执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:58