0

您好我正在尝试从我的 Parse 数据库中查询一些文件,并且我希望根据 updateAt 时间对文件进行排序。我有以下代码。查询有效,结果根据我的条件进行排序,但是当我使用 getDataInBackground 加载文件然后添加到数组时。文件未排序,它们在数组中似乎是随机的。

所以我的问题是

  1. 我该怎么做才能确保数组中的文件与查询结果的顺序相同?
  2. 有什么方法可以根据 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]);
    }



}];
4

1 回答 1

0

使用断点并跟踪第一个 photoImageStacks 和第二个响应后,如果您使用 tableview 或某些委托或触发通知,则应调用 reload 方法,以便您可以在成功响应后相应地更新 ui。

于 2015-12-07T06:48:10.253 回答