我想将来自 Parse 查询的 PFObjects 保存在我的类称为 listdata 的 NSMutableArray 中。我稍后会使用 listdata 数组。当我跟踪我的代码时,它会为找到的每个对象更新 highScoreObjects 数组。但是当我尝试将 listdata 数组设置为 highScoreObjects 数组时,highScoreObjects 数组为空。有没有办法在查询结束后保留数据?
NSMutableArray *highScoreObjects = [[NSMutableArray alloc] initWithCapacity:5];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(@"Successfully retrieved %d scores.", objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
[highScoreObjects addObject:object];
NSLog(@"%@", object.objectId);
}
dispatch_async(dispatch_get_main_queue(), ^ {
[self.tableView reloadData];
});
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
self.listData = highScoreObjects;
我还尝试保留行 self.listData = highScoreObjects;
在 self.listData = highScoreObjects;
循环中。这没有任何区别。
最佳答案
不是没有设置。只是还没有。这是因为您正在使用 findObjectsInBackgroundWithBlock
并且异步过程尚未完成。
将您的分配 (self.listData = highScoreObjects;
) 移动到 block 中,就在您发送重新加载 TableView 的请求之前。
关于ios - 从解析查询中保存 PFObjects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20081481/