I just want to upload multiple image on firebase using swift. I am now uploading one image but unable to upload multiple image. Here is my code
let photoIdString = NSUUID().uuidString
let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOF_REF).child("posts").child(photoIdString)
storageRef.putData(imageData, metadata: nil,completion: {(metadata,error) in
if error != nil {
return
}
let photoUrl = metadata?.downloadURL()?.absoluteString
let ref = Database.database().reference()
let postReference = ref.child("posts")
let newPostId = postReference.childByAutoId().key
let newPostReference = postReference.child(newPostId)
newPostReference.setValue(["photoUrl":photoUrl,"caption":self.textView.text!])
Currently there is no direct API to uploading/downloading the files in batch. We can not use loop because all the tasks perform
asynchronously
. What we can do is to use a recursive function.Core Logic
Full Code Example
1. I created a custom class for file uploading
2. Here how can we use this
First of all create a completion block for main function which will let you know when all images will be uploaded successfully.
Below are two functions first one is the initial one which will start the uploading and the second one is a recursion which will call itself if there is next image available to upload.
And finally here is the main function with completion block
EDIT "upload" function for new Firebase:
The only difference is the way of getting downloading url. Here is the new Firebase doc on same.