ios - 解析iOS PFFile下载顺序

标签 ios objective-c iphone parse-platform for-in-loop

我将 5 个 PFFile 存储在一个数组中,并使用 getDataInBackgroundWithBlock 从 Parse 下载这些文件。

问题是它们在表格 View 单元格中出现的顺序每次都不同,大概是因为文件大小不同而以不同的速度下载。

for (PFFile *imageFile in self.imageFiles) {
  [imageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
    if (!error) {
      UIImage *avatar = [UIImage imageWithData:imageData];
      [self.avatars addObject:avatar];
      cell.userImageView.image = self.avatars[indexPath.row];
    }
  }];
}

self.imageFiles 数组的顺序正确。 如何确保下载的图像按照与 self.imageFiles 相同的顺序添加到 self.avatars 数组中?

最佳答案

问题有两部分:(1) 明确地,如何维护异步操作结果的顺序,(2) 使用 cell 隐含地,如何在支持中正确处理异步请求一个表格 View 。

第一个问题的答案更简单:将请求的结果与请求的参数关联起来。

// change avatars to hold dictionaries associating PFFiles with images
@property(nonatomic,strong) NSMutableArray *avatars; 

// initialize it like this
for (PFFile *imageFile in self.imageFiles) {
    [avatars addObject:[@{@"pfFile":imageFile} mutableCopy]];
}

// now lets factor an avatar fetch into its own method
- (void)avatarForIndexPath:(NSIndexPath *)indexPath completion:^(UIImage *, NSError *)completion {

    // if we fetched already, just return it via the completion block
    UIImage *existingImage = self.avatars[indexPath.row][@"image"];
    if (existingImage) return completion(existingImage, nil);

    PFFile *pfFile = self.avatars[indexPath.row][@"pfFile"];
    [pfFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
        if (!error) {
            UIImage *avatar = [UIImage imageWithData:imageData];
            self.avatars[indexPath.row][@"image"] = avatar;
            completion(avatar, nil);
        } else {
            completion(nil, error);
        }
    }];
}

第 (1) 部分可以。对于第 2 部分,您的 cellForRowAtIndexPath 代码必须识别重复使用单元格。当异步图像获取发生时,您正在处理的单元格可能已经滚动走了。通过不引用完成 block 中的单元格(仅 indexPath)来修复此问题。

    // somewhere in cellForRowAtIndexPath
    // we're ready to setup the cell's image view 

    UIImage *existingImage = self.avatars[indexPath.row][@"image"];
    if (existingImage) {
        cell.userImageView.image = existingImage;
    } else {
        cell.userImageView.image = // you can put a placeholder image here while we do the fetch
        [self avatarForIndexPath:indexPath completion:^(UIImage *image, NSError *error) {
            // here's the trick that is often missed, don't refer to the cell, instead:
            if (!error) {
                [tableView reloadRowsAtIndexPaths:@[indexPath]];
            }
        }];
    }

重新加载完成 block 中的行将导致再次调用 cellForRowAtIndexPath,但后续调用除外,我们将拥有一个现有图像,并且单元格将立即进行配置。

关于ios - 解析iOS PFFile下载顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29867837/

相关文章:

iphone - CoreAudio 节奏变化 (iOS)

ios - CloudKit:查询索引(recordID.recordName)

ios - RESTful UIButton 操作的用户体验

ios - 由图像组成的AVPlayerItem

ios - 全宽图表切断了 ios-charts 中的左右值

ios - Swift2.0 : How to get both return value and error with do try catch

ios - 从一个 UIViewController 转到另一个而不使用导航(segue)iOS 6

iphone - 检测 UIImagePickerController 中的方向变化?

ios - 在不使用 XCode 的情况下协同设计 iOS 应用程序(启用 APNS)

iphone - 我可以在另一个应用程序中拥有一个应用程序吗?