[英]How to finish the upload of images to Firebase Storage and then save the imageUrl to the Firebase Database
I didn't find an answer that satisfied me and hope you have any idea. 我没有找到满意的答案,希望您有任何想法。 I want to upload my images to the Firebase storage and save the imageUrls into the Firebase database.
我想将图像上传到Firebase存储并将imageUrls保存到Firebase数据库。
var imageUrls = [String]()
func uploadImagesToStorage(imagesArray: [UIImage]) {
for i in imagesArray {
guard let uploadData = UIImageJPEGRepresentation(i, 0.3) else { return }
let fileName = NSUUID().uuidString
FIRStorage.storage().reference().child("post_Images").child(fileName).put(uploadData, metadata: nil) { (metadata, err) in
if let err = err {
return
}
guard let profileImageUrl = metadata?.downloadURL()?.absoluteString else { return }
self.imageUrls.append(profileImageUrl)
}.resume()
} //..End loop
saveToDatabaseWithImageUrl(imageUrls: imageUrls)
Uploading the images works with the uploadImagesToStorage(imagesArray: [UIImage]) method. 上载图像可以使用uploadImagesToStorage(imagesArray:[UIImage])方法。 This method gets an array as argument which contains the images that I want to upload.
此方法获取一个数组作为参数,其中包含我要上传的图像。 While uploading the images I'm downloading the imageUrl information from the metadata that firebase is giving me and save that imageUrl into the imageUrls array.
在上传图像时,我正在从firebase给我的元数据中下载imageUrl信息,并将该imageUrl保存到imageUrls数组中。 For loop is necessary to save the imageUrl information for every single image.
为了保存每个图像的imageUrl信息,必须进行for循环。 When the images are uploaded and the imageUrls Array is filled with the imageUrl information I call the function func saveToDatabaseWithImageUrl(imageUrls: [String]) to save the imageUrls into the database.
当上传图像并在imageUrls数组中填充了imageUrl信息时,我调用函数func saveToDatabaseWithImageUrl(imageUrls:[String])将imageUrls保存到数据库中。 Checking Firebase I see that the images are saved into the Firebase storage, but the imageUrls are not saved into the Firebase database.
检查Firebase我看到图像已保存到Firebase存储中,但是imageUrls没有保存到Firebase数据库中。 While debugging my code I found out that the reason for this behavior is that while the images are uploaded the code jumps to the next line.
在调试我的代码时,我发现这种现象的原因是,在上传图像时,代码跳到了下一行。 So it calls the saveToDatabaseWithImageUrls with an empty imageUrls array.
因此,它将使用一个空的imageUrls数组调用saveToDatabaseWithImageUrls。 I read the [Documentation][1] and tried to manage the upload with the .resume() method.
我阅读了[Documentation] [1],并尝试使用.resume()方法管理上传。 Still it jumped to the saveToDatabaseWithImageUrl method.
仍然跳转到saveToDatabaseWithImageUrl方法。 I don't know how to guarantee that the upload is finished and then the next line of code is executed.
我不知道如何保证上传完成,然后执行下一行代码。 Thanks for your help.
谢谢你的帮助。
Its happen because success block of your .child("post_Images").child(fileName).put
call asynchronously. 发生这种情况是因为您的
.child("post_Images").child(fileName).put
调用异步进行。 Rest of code go sync. 其余代码同步。 So your
for
start uploading photos and after that you are saving URLs to database but urls are empty because you don't wait for finish uploading. 因此,您
for
开始上传照片,然后将URL保存到数据库中,但是URL为空,因为您无需等待完成上传。
I give you a perfect solution based on DispathGroup
我给你一个基于
DispathGroup
的完美解决方案
//Create DispatchGroup
let fetchGroup = DispatchGroup()
for i in imagesArray {
guard let uploadData = UIImageJPEGRepresentation(i, 0.3) else { return }
let fileName = NSUUID().uuidString
//Before every iteration enter to group
fetchGroup.enter()
FIRStorage.storage().reference().child("post_Images").child(fileName).put(uploadData, metadata: nil) { (metadata, err) in
if let err = err {
fetchGroup.leave()
return
}
guard let profileImageUrl = metadata?.downloadURL()?.absoluteString else { return }
self.imageUrls.append(profileImageUrl)
//after every completion asynchronously task leave the group
fetchGroup.leave()
}.resume()
}
And know id magic 并知道id魔术
fetchGroup.notify(queue: DispatchQueue.main) {
//this code will call when number of enter of group will be equal to number of leaves from group
//save your url here
saveToDatabaseWithImageUrl(imageUrls: imageUrls)
}
This solution don't block a thread, everything works asynchronously here. 此解决方案不会阻塞线程,此处所有内容均异步运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.