使用 Parse 在后台下载多个文件
Downloading multiple files in background using Parse
我的问题:知道最后一张图片何时下载的最佳方法是什么,因为一旦所有文件都下载完毕,我需要更新 UI。
我正在从 Parse 下载一组 PFFile。我现在正在做的是:以这种方式从解析中获取 PFFiles 数组
func getAllUserPictures(completion: (imageFiles: [PFFile]) -> Void) {
guard let id = currentUserID else {
return
}
let query = PFQuery(className: "Profile")
query.whereKey("objectId", equalTo: id)
query.includeKey("avatars")
query.findObjectsInBackgroundWithBlock { profile, error in
guard let imageFiles = profile?.first?.objectForKey("avatars") as? [PFFile] else {
return
}
completion(imageFiles: imageFiles)
}
}
从完成处理程序获得 imageFile 数组后,我继续以这种方式下载它们:
func getUserPicturesWithBlock(completion: (result: [UIImage]) -> Void) {
DataManager.sharedInstance.getAllUserPictures { imageFiles in
for file in imageFiles {
file.getDataInBackgroundWithBlock({ data, error in
guard let data = data, let image = UIImage (data: data) else{
return
}
self.imagesArray.append(image)
})
}
}
}
您是否考虑过使用 ParseUI
框架中的多个 PFImageView
?
您可以在完成查询时将每个 PFFile 分配给一个 PFImageView,然后在每个 PFImageView 上调用 loadInBackground
。 PFImageView
class 会在数据加载后自动处理更新图像,这也会在获取图像后立即更新每个图像,而不是等待所有图像一起。
或者,为了保持与现有结构相似的结构,您可以使用 imageFiles 数组的大小来判断它们何时全部加载完毕。在每个文件加载完成后,您可以检查加载的图像数量是否等于数组的总大小。
我的问题:知道最后一张图片何时下载的最佳方法是什么,因为一旦所有文件都下载完毕,我需要更新 UI。
我正在从 Parse 下载一组 PFFile。我现在正在做的是:以这种方式从解析中获取 PFFiles 数组
func getAllUserPictures(completion: (imageFiles: [PFFile]) -> Void) {
guard let id = currentUserID else {
return
}
let query = PFQuery(className: "Profile")
query.whereKey("objectId", equalTo: id)
query.includeKey("avatars")
query.findObjectsInBackgroundWithBlock { profile, error in
guard let imageFiles = profile?.first?.objectForKey("avatars") as? [PFFile] else {
return
}
completion(imageFiles: imageFiles)
}
}
从完成处理程序获得 imageFile 数组后,我继续以这种方式下载它们:
func getUserPicturesWithBlock(completion: (result: [UIImage]) -> Void) {
DataManager.sharedInstance.getAllUserPictures { imageFiles in
for file in imageFiles {
file.getDataInBackgroundWithBlock({ data, error in
guard let data = data, let image = UIImage (data: data) else{
return
}
self.imagesArray.append(image)
})
}
}
}
您是否考虑过使用 ParseUI
框架中的多个 PFImageView
?
您可以在完成查询时将每个 PFFile 分配给一个 PFImageView,然后在每个 PFImageView 上调用 loadInBackground
。 PFImageView
class 会在数据加载后自动处理更新图像,这也会在获取图像后立即更新每个图像,而不是等待所有图像一起。
或者,为了保持与现有结构相似的结构,您可以使用 imageFiles 数组的大小来判断它们何时全部加载完毕。在每个文件加载完成后,您可以检查加载的图像数量是否等于数组的总大小。