• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 解析PFFile下载顺序iOS

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:08:19 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我将 5 个 PFFiles 存储在一个数组中,并使用 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 数组中?



Best Answer-推荐答案


问题有两部分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)avatarForIndexPathNSIndexPath *)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 - 解析PFFile下载顺序iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29867837/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap