【问题标题】:Ignore fetching if PFQuery is already running如果 PFQuery 已经在运行,则忽略获取
【发布时间】:2014-11-28 01:28:16
【问题描述】:

如果一个已经在运行,我怎么能忽略新的提取。这是我的代码的一个示例: 因此,如果我调用 [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;
    }];
}

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 哦,非常好。我不在乎它是否更简单。我想以正确的方式去做。非常感谢。
    猜你喜欢
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多