gpt4 book ai didi

swift - Firebase连续上传中途停止,只上传最后一张图片

转载 作者:行者123 更新时间:2023-11-28 08:04:41 24 4
gpt4 key购买 nike

我在使用 Swift 将图像数组上传到 Firebase 存储时遇到问题。我有这个功能:

func uploadImages(ref: FIRStorageReference, data: Data, completionHandler: @escaping imagesHandler) {

let task = ref.put(data)

let observer = task.observe(.progress) { (snapshot) in
print(snapshot.progress!)
}

task.observe(.success) { (snapshot) in
if let url = snapshot.metadata {

if let link = url.downloadURL()?.absoluteString {

completionHandler(link)
}else {

completionHandler(nil)
}
}else {

completionHandler(nil)
}
}
}

我在这里使用:

var downloadURLImages = [String]()

for each in itemImage {

self.uploadImages(ref: self.inventoryStorageRef, data: each, completionHandler: { (links) in
if let url = links {

downloadURLImages.append(url)
}
})
}

问题是,我得到了下载 URL,但它只上传了数组的最后一张图片。在第一个函数中,我放置了一个观察者来查看发生了什么,下面是结果。

上传了 2 张图片:

<NSProgress: 0x60000052b2c0> : Parent: 0x0 / Fraction completed: 0.0000 / Completed: 0 of 41605  
<NSProgress: 0x60000052a5a0> : Parent: 0x0 / Fraction completed: 0.0026 / Completed: 108 of 41713
<NSProgress: 0x608000135f40> : Parent: 0x0 / Fraction completed: 0.1990 / Completed: 8300 of 41713
<NSProgress: 0x608000135f40> : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 41713 of 41713
<NSProgress: 0x60000052a640> : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 41713 of 41713

上传了 4 张图片:

<NSProgress: 0x600000520280> : Parent: 0x0 / Fraction completed: 0.0000 / Completed: 0 of 41605  
<NSProgress: 0x600000527580> : Parent: 0x0 / Fraction completed: 0.0000 / Completed: 0 of 29438
<NSProgress: 0x60000052b5e0> : Parent: 0x0 / Fraction completed: 0.0000 / Completed: 0 of 29438
<NSProgress: 0x60000052a5a0> : Parent: 0x0 / Fraction completed: 0.0000 / Completed: 0 of 41605
<NSProgress: 0x600000527800> : Parent: 0x0 / Fraction completed: 0.0000 / Completed: 0 of 41605
<NSProgress: 0x60000052b7c0> : Parent: 0x0 / Fraction completed: 0.0026 / Completed: 108 of 41713
<NSProgress: 0x60000052aaa0> : Parent: 0x0 / Fraction completed: 0.0037 / Completed: 108 of 29546
<NSProgress: 0x60000052b7c0> : Parent: 0x0 / Fraction completed: 0.0037 / Completed: 108 of 29546
<NSProgress: 0x60000052ab40> : Parent: 0x0 / Fraction completed: 0.0026 / Completed: 108 of 41713
<NSProgress: 0x60000052ab40> : Parent: 0x0 / Fraction completed: 0.0026 / Completed: 108 of 41713
<NSProgress: 0x60000052c1c0> : Parent: 0x0 / Fraction completed: 0.1990 / Completed: 8300 of 41713
<NSProgress: 0x60000052c1c0> : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 41713 of 41713
<NSProgress: 0x60000052c1c0> : Parent: 0x0 / Fraction completed: 0.1990 / Completed: 8300 of 41713
<NSProgress: 0x6080001381a0> : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 41713 of 41713
<NSProgress: 0x60000052c6c0> : Parent: 0x0 / Fraction completed: 0.2809 / Completed: 8300 of 29546
<NSProgress: 0x60000052c6c0> : Parent: 0x0 / Fraction completed: 0.1990 / Completed: 8300 of 41713
<NSProgress: 0x60000052c760> : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 41713 of 41713
<NSProgress: 0x608000139000> : Parent: 0x0 / Fraction completed: 0.2809 / Completed: 8300 of 29546
<NSProgress: 0x608000136ee0> : Parent: 0x0 / Fraction completed: 0.2809 / Completed: 8300 of 29546
<NSProgress: 0x60000052b180> : Parent: 0x0 / Fraction completed: 0.2809 / Completed: 8300 of 29546
<NSProgress: 0x60000052c120> : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 41713 of 41713
<NSProgress: 0x608000139280> : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 41713 of 41713
<NSProgress: 0x60000052bb80> : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 41713 of 41713

只有最后一张图片完整上传。

我该怎么办?

最佳答案

尝试下面的代码

 func uploadImages(ref: FIRStorageReference, data: Data, completionHandler: @escaping imagesHandler) {
let imageName = NSUUID().uuidString
let task = ref.child("\(imageName).png").putData(uploadData, metadata: nil, completion: { (snapshot, error) in
if let error = error {
print(error)
completionHandler(nil)
}
})

let observer = task.observe(.progress) { (snapshot) in
print(snapshot.progress!)
}

task.observe(.success) { (snapshot) in
if let url = snapshot.metadata {

if let link = url.downloadURL()?.absoluteString {

completionHandler(link)
}else {

completionHandler(nil)
}
}else {

completionHandler(nil)
}
}
}

关于swift - Firebase连续上传中途停止,只上传最后一张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45517824/

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