您好我正在尝试从我的 Parse 数据库中查询一些文件,并且我希望根据 updateAt 时间对文件进行排序。我有以下代码。查询有效,结果根据我的条件进行排序,但是当我使用 getDataInBackground 加载文件然后添加到数组时。文件未排序,它们在数组中似乎是随机的。
所以我的问题是
- 我该怎么做才能确保数组中的文件与查询结果的顺序相同?
- 有什么方法可以根据 getDataInBackground 完成块中的 objectID 检查文件/图像?
ps 我不想使用getData,因为我不希望它阻塞主线程。
非常感谢您提前
PFQuery *query = [PFQuery queryWithClassName:@"Photo"];
[query orderByDescending:@"updateAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *photoStacks, NSError *error)
{
if (!error) {
// The find succeeded.
for (PFObject *photoImage in photoStacks) {
PFFile *userImageFile = photoImage[@"image"];
[userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:imageData];
// need to check object id before adding into the stack to make sure the order is right
[photoImageStacks addObject:image];
if ([photoImageStacks count] == photoStacksCount)
{
[photoPile setArray:photoImageStacks];
}
}
}];
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];