ios – findObjectsInBackgroundWithBlock:从Parse获取数据,但数据仅存在于块内

我做了以下测试类来尝试从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文档,但我仍然不确定我做错了什么.

解决方法

最后一个NSLog(@“字典是%@”,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,则需要在主线程上运行.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...