findObjectsInBackgroundWithBlock:从解析获取数据,但数据只存在块内 [英] findObjectsInBackgroundWithBlock: gets data from Parse, but data only exists inside the block

查看:396
本文介绍了findObjectsInBackgroundWithBlock:从解析获取数据,但数据只存在块内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了以下测试类尝试从parse检索数据:

I made the following test class to try out retrieving data from 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...
}

每code里面的注释部分,当我打印 self.scoreDictionary 的code里面,它的作品了罚款,我看到我的整个字典,因为它逐渐被填补。然而,该块结束后,当我再次打印字典,它现在是空的。我是双查询API文档检查,但我还是不能确定我在做什么错误。

Per the commented section inside the code, when I print self.scoreDictionary inside the code, it works out fine, and I see my entire dictionary as it incrementally gets filled. However, after the block ends, when I print the dictionary again, it is now empty. I double checked with the query API docs, but I still am unsure what I am doing incorrectly.

推荐答案

最后一个的NSLog(@这本字典%@,self.scoreDictionary)语句不块完成后,实际执行。它的 findObjectsInBackgroundWithBlock 方法返回后执行。 findObjectsInBackgroundWithBlock presumably运行在一个单独的线程的东西,你的块不可以实际执行可言,直到去年的NSLog语句之后的一段时间内。从图形上看,这样的事情很可能发生的事情:

The last NSLog(@"The dictionary is %@", self.scoreDictionary) statement does not actually execute after the block completes. It executes after the findObjectsInBackgroundWithBlock method returns. findObjectsInBackgroundWithBlock presumably runs something in a separate thread, and your block may not actually execute at all until some length of time after that last NSLog statement. Graphically, something like this is probably happening:

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,调用其他方法,等等?你会想这样做的的你的格挡,在这一点上,你知道数据已成功提取。例如,如果你有你想重装表视图,你可以这样做:

You probably want to think about, what do you want to do with your scoreDictionary data after you have retrieved it? For example, do you want to update the UI, call some other method, etc.? You will want to do this inside your block, at which point you know the data has been successfully retrieved. For example, if you had a table view you wanted to reload, you could do this:

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

请注意在 dispatch_async - 如果你需要更新你的数据后做的工作就是改变用户界面,你会想,要在主线程上运行

Note the dispatch_async - if the work you need to do after updating your data involves changing the UI, you'll want that to run on the main thread.

这篇关于findObjectsInBackgroundWithBlock:从解析获取数据,但数据只存在块内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆