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