如果PFQuery已在运行,则忽略提取

时间:2014-11-28 01:28:17

标签: ios parse-platform objective-c-blocks

如果已经在运行,我怎么能忽略新的提取。这是我的代码的一个例子: 所以如果我打电话给[self getParticipants]如何确保忽略已经运行。我唯一的解决方案是创建BOOL属性" inMiddleOfFetching"但我不想为此创建另一个BOOL属性。有更好的解决方案吗?

- (void)getParticipants {
    PFQuery *participantsQuery = [self.participantsRelation query];
    [participantsQuery includeKey:@"client"];
    [participantsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects)
            self.participants = objects;
    }];
}

1 个答案:

答案 0 :(得分:3)

这并不简单但是更好,您可以使用螺栓为查询创建单个任务,这样如果多次调用它只会运行一次,但所有调用都会同时返回值。 像这样:

 @property BFTask* task;
- (BFTask *)getParticipants {
if (!_task) {
    PFQuery *participantsQuery = [self.participantsRelation query];
    [participantsQuery includeKey:@"client"];
    _task = [participantsQuery findObjectsInBackground];
}

return _task;

}

然后得到结果:

[[self getParticipants] continueWithBlock:^id(BFTask *task) {
    if(!task.error){
        self.participants = task.result;
    }
    _task = nil; //if you want to run the query again in the future
    return nil;
}];