gpt4 book ai didi

swift - 这是显示异步数据的好方法吗?

转载 作者:可可西里 更新时间:2023-11-01 01:25:34 24 4
gpt4 key购买 nike

我是异步编码的新手,想知道我用来获取和显示数据的方法在 swift 中是否被认为是正确的。

此方法从数据库中用户的部分获取对象列表,然后为列表中的每个项目获取图片。我判断是否已获取所有图像的方式是在它们到达时将它们推送到一个数组,然后如果该数组的长度等于我重新加载 View 的对象列表。这是我的代码:

var pets = [String]()
var imgs = [UIImage]()

override func viewDidAppear(_ animated: Bool) {
imgsLoaded = 0
imgs.removeAll(keepingCapacity: false) // Clear images array
pets.removeAll(keepingCapacity: false) // Clear list of objects

let userRef = FIRDatabase.database().reference().child("users").child((FIRAuth.auth()?.currentUser?.uid)!).child("pets")

userRef.observeSingleEvent(of: .value, with: { snapshot in // fetch list of objects from database
if (snapshot.value as? Bool) != nil { // User has no pets added
self.loadScrollView() // Reload view
} else if let snap = snapshot.value as? NSDictionary { // User has pets
for value in snap {
self.pets.append(value.key as! String) // Append object to list of objects
}
for i in 0..<self.pets.count { // For each item in the list, fetch its corresponding image
let imgRef = FIRStorage.storage().reference().child("profile_images").child(self.pets[i]+".png")
imgRef.data(withMaxSize: 15 * 1024 * 1024) { (data, error) -> Void in
if error != nil {
print(error)
}

// Create a UIImage, add it to the array
self.imgs.append(UIImage(data: data!)!) // Push image to list of images
self.imgsLoaded+=1
if self.imgsLoaded == self.pets.count { // If same number of images loaded, reload the view
self.loadScrollView()
}
}
}
}
})
}

正如我所说,我是异步编码的新手,想听听我尝试的正确方法是什么。我的方法的一个问题是图像数组可能与数据数组不对齐,因为可以乱序获取图像。我很想了解执行此操作的最佳方法,所以请告诉我!

编辑:确保我的数据与相应图像对齐的一种方法是设置一个字典,其中键是数据,值是我猜的图像。

最佳答案

我建议使用 DispatchGroup 而不是计算 2 个数组中的项目数。这也将允许跟踪多个线程的进度,每个 block 在组中注册了 enter()leave()。然后,一旦所有 block 都进入并离开,就会调用 notify block ,这可以刷新您的 UI。还使用带有占位符图像的字典,以防加载其中一张图像失败。比忽略失败案例要好。

这也可能更容易阅读和推理,因为没有跟踪 2 个数组的计数。 enter()leave()

的意图更清晰
var imgs = [String: UIImage]()
var dispatchGroup = DispatchGroup()

override func viewDidAppear(_ animated: Bool) {

let userRef = FIRDatabase.database().reference().child("users").child((FIRAuth.auth()?.currentUser?.uid)!).child("pets")

userRef.observeSingleEvent(of: .value, with: { [weak self] snapshot in // fetch list of objects from database
if (snapshot.value as? Bool) != nil { // User has no pets added
self?.loadScrollView() // Reload view
} else if let snap = snapshot.value as? NSDictionary { // User has pets
for value in snap {
self?.dispatchGroup.enter()
let imgRef = FIRStorage.storage().reference().child("profile_images").child(self!.pets[i]+".png")
imgRef.data(withMaxSize: 15 * 1024 * 1024) { (data, error) -> Void in
if let error = error {
print(error) // handle the error here but still call .leave() on the group to be sure the notify block is called.
imgs[value] = somePlaceHolderImage
self?.dispatchGroup.leave()
} else if let data = data, let image = UIImage(data: data) {
imgs[value] = image
self?.dispatchGroup.leave()
}

dispatchGroup.notify(queue: DispatchQueue.main, execute: {
self?.loadScrollView()
})
}
}
}
})
}

关于swift - 这是显示异步数据的好方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41772601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com