Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.91 KB | None | 0 0
  1. //This is what you're doing
  2.  
  3. upload(image: image!, to: "Users/\(Auth.auth().currentUser!.uid)/photoUrls/0", completion: { urlString, error in
  4.                 Database.database().reference().child("Users/\(Auth.auth().currentUser!.uid)/photoUrls/0").setValue(urlString)
  5.             },
  6.                 Database.database().reference().child("Users/\(Auth.auth().currentUser!.uid)/photoUrls/0").observeSingleEvent(of: .value) { (snapshot) in
  7.                     let urlString = snapshot.value as! String
  8.                     let task = URLSession.shared.dataTask(with: URL(string: urlString)!) {(data, response, error) in
  9.  
  10.                         if let image: UIImage = UIImage(data: data!) {
  11.                             DispatchQueue.main.async {
  12.                                 //update the image
  13.                                 self.ProfileImage.image = image
  14.                             }
  15.                         }
  16.                     }
  17.                     task.resume()
  18.                 }
  19.                    )}
  20.  
  21.  
  22.  
  23. //This is what you should do:
  24. //Upload the image
  25. upload(image: image!, to: "Users/\(Auth.auth().currentUser!.uid)/photoUrls/0", completion: {
  26.     urlString, error in
  27.     Database.database().reference().child("Users/\(Auth.auth().currentUser!.uid)/photoUrls/0").setValue(urlString)
  28. })
  29.  
  30. //Grab the image (it won't be available right away as "upload" is an asynchronous function)
  31. Database.database().reference().child("Users/\(Auth.auth().currentUser!.uid)/photoUrls/0").observeSingleEvent(of: .value) {
  32.     (snapshot) in
  33.     let urlString = snapshot.value as! String
  34.     let task = URLSession.shared.dataTask(with: URL(string: urlString)!) {
  35.         (data, response, error) in
  36.         if let image: UIImage = UIImage(data: data!) {
  37.             DispatchQueue.main.async {
  38.                 //update the image
  39.                 self.ProfileImage.image = image
  40.             }
  41.         }
  42.     }
  43.  
  44.     task.resume()
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement