Firebase 未按正确顺序下载 gif - iOS
Firebase not downloading gifs in correct order - iOS
所以我使用 Firebase 存储存储了 4 个 gif,我正在使用实时数据库获取它们的下载 URL 并将它们附加到一个数组中。问题是,当我想通过遍历数组来下载它们时,它们并不总是以正确的顺序下载。事实上,它每次都以随机顺序下载。
到目前为止我的代码:
if self.downloadURLS.isEmpty == true {
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
var newItems: [UIImage] = [UIImage]()
for (index, element) in self.downloadURLS.enumerate() {
print("Item \(index): \(element)")
self.storageRef = self.storage.referenceForURL(element)
self.storageRef.dataWithMaxSize(5 * 1024 * 1024) { (data, error) -> Void in
if (error != nil) {
// Uh-oh, an error occurred!
} else {
self.image = UIImage(gifData: data!, levelOfIntegrity: 1)
print("Downloaded \(index)")
newItems.append(self.image)
}
}
self.items = newItems
}
}
print(downloadURLS.joinWithSeparator("\n"))
}
有什么方法可以确保它们的下载顺序正确,或者我可以在最后使用某种排序方法吗?上次完成时,它按顺序下载:2,3,0,1...任何帮助都会很棒!
我猜你的处理程序是异步调用的。我建议你预先声明你的 newItems
数组并添加一个 dispatch_group_t
来跟踪每张图片的下载时间:
var newItems = [UIImage?](count: downloadURLS.count, repeatedValue: nil)
let group = dispatch_group_create()
for (index, element) in self.downloadURLS.enumerate() {
dispatch_group_enter(group)
self.storageRef = self.storage.referenceForURL(element)
self.storageRef.dataWithMaxSize(5 * 1024 * 1024) { (data, error) -> Void in
if (error != nil) {
// element at this index stays nil
} else {
// Set the element at this index
newItems[index] = UIImage(gifData: data!, levelOfIntegrity: 1)
}
dispatch_group_leave(group)
}
}
// The following gets called asynchronously when every image has been downloaded (or failed to)
dispatch_group_notify(group, dispatch_get_main_queue()) {
// This will throw an error because newItems is now an array of Optional UIImages
// It contains nil when there was an error while getting a specific item
self.items = newItems
}
另外:在 for
循环中分配 self.storageRef
是否正确?我相信您应该改用局部变量:
let ref = self.storage.referenceForURL(element)
ref.dataWithMaxSize ...
所以我使用 Firebase 存储存储了 4 个 gif,我正在使用实时数据库获取它们的下载 URL 并将它们附加到一个数组中。问题是,当我想通过遍历数组来下载它们时,它们并不总是以正确的顺序下载。事实上,它每次都以随机顺序下载。
到目前为止我的代码:
if self.downloadURLS.isEmpty == true {
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
var newItems: [UIImage] = [UIImage]()
for (index, element) in self.downloadURLS.enumerate() {
print("Item \(index): \(element)")
self.storageRef = self.storage.referenceForURL(element)
self.storageRef.dataWithMaxSize(5 * 1024 * 1024) { (data, error) -> Void in
if (error != nil) {
// Uh-oh, an error occurred!
} else {
self.image = UIImage(gifData: data!, levelOfIntegrity: 1)
print("Downloaded \(index)")
newItems.append(self.image)
}
}
self.items = newItems
}
}
print(downloadURLS.joinWithSeparator("\n"))
}
有什么方法可以确保它们的下载顺序正确,或者我可以在最后使用某种排序方法吗?上次完成时,它按顺序下载:2,3,0,1...任何帮助都会很棒!
我猜你的处理程序是异步调用的。我建议你预先声明你的 newItems
数组并添加一个 dispatch_group_t
来跟踪每张图片的下载时间:
var newItems = [UIImage?](count: downloadURLS.count, repeatedValue: nil)
let group = dispatch_group_create()
for (index, element) in self.downloadURLS.enumerate() {
dispatch_group_enter(group)
self.storageRef = self.storage.referenceForURL(element)
self.storageRef.dataWithMaxSize(5 * 1024 * 1024) { (data, error) -> Void in
if (error != nil) {
// element at this index stays nil
} else {
// Set the element at this index
newItems[index] = UIImage(gifData: data!, levelOfIntegrity: 1)
}
dispatch_group_leave(group)
}
}
// The following gets called asynchronously when every image has been downloaded (or failed to)
dispatch_group_notify(group, dispatch_get_main_queue()) {
// This will throw an error because newItems is now an array of Optional UIImages
// It contains nil when there was an error while getting a specific item
self.items = newItems
}
另外:在 for
循环中分配 self.storageRef
是否正确?我相信您应该改用局部变量:
let ref = self.storage.referenceForURL(element)
ref.dataWithMaxSize ...