Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference Firebase users in database with images in Firebase Storage Swift

Normally I would be able to find an answer to this question online but since its so new I have been having trouble.

When I have users sign into the app and they choose 4-5 pictures for their profile, how do I store those images in Firebase Storage and reference those images to that user in Firebase Database?

Thanks

like image 460
JustinM Avatar asked Jun 07 '16 08:06

JustinM


1 Answers

You upload them to the Firebase Storage first and then store the url in Firebase Database

let storage = FIRStorage.storage()
let data: NSData = myImageData
let userProfilePic = storageRef.child("users/abc/profileimage.jpg")

let uploadTask = userProfilePic.putData(data, metadata: nil) { metadata, error in
  if (error != nil) {
    // Uh-oh, an error occurred!
  } else {
    let downloadURL = metadata!.downloadURL
    // store downloadURL in db
    storeUserProfileInDB(downloadURL)
  }
}

func storeUserProfileInDB(profileImgUrl: NSURL) {
    let ref = FIRDatabase.database().reference()
    let key = ref.child("users").childByAutoId().key

    let dictionaryUser = [ "userName"    : name! ,
                           "imageUrl" : profileImgUrl.absoluteString,
                           ]

    let childUpdates = ["/users/\(key)": dictionaryTodo]
    ref.updateChildValues(childUpdates, withCompletionBlock: { (error, ref) -> Void in
        //save
    })

}
like image 127
Shubhank Avatar answered Sep 28 '22 02:09

Shubhank