Reputation:
I am trying to call data from firebase that changes the image view to be that of what is stored in the database. when I call my code, I see
let image: UIImageView = {
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String : Any] else {
return }
let user = MyUser(dictionary: dictionary as [String : AnyObject])
let profileImageView = UIImageView()
profileImageView.translatesAutoresizingMaskIntoConstraints = false
profileImageView.contentMode = .scaleAspectFill
profileImageView.layer.cornerRadius = 30
profileImageView.clipsToBounds = true
profileImageView.image = UIImage(named: "app")
profileImageView.image=#imageLiteral(resourceName: "user")
profileImageView.loadImageUsingCacheWithUrlString(user.profileImageUrl!)
return profileImageView
}, withCancel: { (err) in
print("attempting to load information")
})
// return profileImageView
}()
where the commented out return function is, I am getting the error Use of unresolved identifier 'profileImageView'; did you mean 'provideImageData'? why? I would assume that since the return is within the closures, it would know what profileImageView is.
Upvotes: 1
Views: 84
Reputation: 949
Separate the image view and the real time database listener:
let imageView: UIImageView {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = 30
imageView.clipsToBounds = true
return imageView
}
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String : Any] else {
return }
let user = MyUser(dictionary: dictionary as [String : AnyObject])
// Set the image view's image
imageView.image = // ...
}, withCancel: { (err) in
print("attempting to load information")
})
Upvotes: 1