在我的Apple Watch应用程序中,我有一个大约25行的表,每一行都有一些文本和一个需要从互联网上加载的图像.类似于Instagram风格的Feed,但这些是每张约8k的个人资料图片.
当我基于传入的JSON数据构建表时,我正在利用WatchKit的内置图像缓存正确地做所有事情,以减少不必要的网络流量.
该应用程序"工作",图像显示正确.但问题是视图需要10-20秒才能为用户进行交互做好准备.在那些10-20秒之前,滚动缓慢,并且在所有图像都已加载之前按钮不会执行任何操作.
我想要的是在用户向下滚动时延迟加载图像的一些方法.
我已经尝试过实现一个分页解决方案,但它也有缺点,并没有完全解决我原来的问题.
如果我只填充文本位(无图像),表格会立即显示并准备好进行交互.
***更新:Mike Swanson的回答非常彻底,确实解决了我的大部分问题.缓慢和阻碍已经消失.
我想发布最终代码给任何其他人,并有类似的问题.具体来说,我想展示我是如何实现后台调度的.
我现在还在主线程中暂时将图像设置为占位符以显示活动.一旦实际图像加载并传输到手表,它就会被替换.如果上传了新图像,我还改进了图像缓存键.
新代码:
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
i = 0
for p in self.profiles {
if let profileId = p["id"] as? NSNumber {
var profileIdStr = "\(profileId)"
let row = self.profilesTable.rowControllerAtIndex(i) as! WatchProfileRow
if let hasImage = p["has_image"] as? NSNumber {
let profileImageCacheKey = "\(profileIdStr)-\(hasImage)"
// if the image is already in cache, show it
if self.device.cachedImages[profileImageCacheKey] != nil {
row.profileImageThumbnail.setImageNamed(profileImageCacheKey)
// otherwise, get the image …Run Code Online (Sandbox Code Playgroud)