7

我做了以下测试类来尝试从 Parse 中检索数据:

-(void)retrieveDataFromParse
{
    PFQuery *query = [PFQuery queryWithClassName:@"TestObject"];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if(!error){
            for (PFObject *object in objects){
                NSString *nameFromObject = [NSString stringWithFormat:@"%@", [object objectForKey:@"Name"]];
                NSString *dateFromObject = [NSString stringWithFormat:@"%@", [object createdAt]];
                NSString *scoreFromObject = [NSString stringWithFormat:@"%@", [object objectForKey:@"Score"]];
                [self addNewScore:scoreFromObject andDate:dateFromObject forUserName:nameFromObject];
                NSLog(@"The dictionary is %@", self.scoreDictionary); //<-- here it works printing out the whole dictionary
            }
        } else {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
    NSLog(@"The dictionary is %@", self.scoreDictionary); //<- but after the block is called, here the dictionary is again empty...
}

根据代码中的注释部分,当我在代码中打印时self.scoreDictionary,它运行良好,并且我看到我的整个字典逐渐被填充。但是,在块结束后,当我再次打印字典时,它现在是空的。我仔细检查了查询 API 文档,但我仍然不确定我做错了什么。

4

2 回答 2

13

块完成后,最后一条NSLog(@"The dictionary is %@", self.scoreDictionary)语句实际上并没有执行。它在findObjectsInBackgroundWithBlock方法返回后执行。findObjectsInBackgroundWithBlock大概在一个单独的线程中运行某些东西,并且在最后一个 NSLog 语句之后的一段时间内,您的块可能根本不会真正执行。从图形上看,可能正在发生这样的事情:

Thread 1 
--------
retriveDataFromParse called
invoke findObjectsInBackgroundWithBlock
findObjectsInBackgroundWithBlock queues up work on another thread
findObjectsInBackgroundWithBlock returns immediately      |
NSLog statement - self.scoreDictionary not yet updated    |
retriveDataFromParse returns                              |
.                                                         V
.                       Thread 2, starting X milliseconds later
.                       --------
.                       findObjectsInBackgroundWithBlock does some work
.                       your block is called
.                       for-loop in your block
.                       Now self.scoreDictionary has some data
.                       NSLog statement inside your block

您可能想考虑一下,在您检索到 scoreDictionary 数据后,您想如何处理它?例如,您是否要更新 UI、调用其他方法等?您将希望在您的块执行此操作,此时您知道数据已成功检索。例如,如果您有一个想要重新加载的表视图,您可以这样做:

for (PFObject *object in objects){
    ....
}
dispatch_async(dispatch_get_main_queue(), ^{
    [self updateMyUserInterfaceOrSomething];
});

请注意dispatch_async- 如果您在更新数据后需要做的工作涉及更改 UI,您会希望它在主线程上运行。

于 2013-08-02T16:30:24.843 回答
0

最后一个NSLog(@"The dictionary is %@", self.scoreDictionary)在完成块执行之前执行。到时候,self.scoreDictionary肯定是空的。

此外,完成块将在主线程上执行。您可以参考以下链接。

https://parse.com/questions/what-thread-does-findobjectsinbackgroundwithblock-complete-on

于 2015-05-08T07:05:52.907 回答