我正在尝试实现更新用户个人资料图片的功能,这反过来将更新(替换)Firebase 中的当前图片文件。由于某种原因,putData 方法没有在我的函数中执行(它没有运行打印语句“code got here”)。我不确定为什么此代码未运行或未完成该功能。提前致谢!
// edit profile viewController
var databaseRef: DatabaseReference!
var storageRef: StorageReference!
override func viewDidLoad() {
databaseRef = Database.database().reference()
storageRef = Storage.storage().reference()
loadProfileImage()
}
func handleSave() {
// save profile info to firebase
updateUsersProfile()
dismiss(animated: true, completion: nil)
}
// update picture func
func updateUsersProfile(){
//check to see if the user is logged in
if let userID = Auth.auth().currentUser?.uid {
//create an access point for the Firebase storage
let storageItem = storageRef.child("profile_images").child(userID)
//get the image uploaded from photo library
guard let image = profileImageView.image else {return}
if let newImage = UIImagePNGRepresentation(image) {
//upload to firebase storage
// code never executes this putData block ------- ?????
storageItem.putData(newImage, metadata: nil, completion: { (metadata, error) in
print("code got here")
if error != nil{
print(error!)
return
}
storageItem.downloadURL(completion: { (url, error) in
if error != nil{
print(error!)
return
}
if let profileImageUrl = metadata?.downloadURL()?.absoluteString {
let newValuesForProfile =
["profileImageUrl": profileImageUrl]
//update the firebase database for that user
self.databaseRef.child("users").child(userID).updateChildValues(newValuesForProfile, withCompletionBlock: { (error, ref) in
if error != nil{
print(error!)
return
}
print("Profile Successfully Update")
})
}
})
})
}
}
}
最佳答案
我使用的是最新的 Xcode 10.2 和 Swift 5。 我到处搜索这个问题,最后,我可以用这个技巧解决它:只需将 podfile 中的“FirebaseInstanceID”修复为版本“2.0.0”
pod 'Firebase'
pod 'Firebase/Core'
pod 'Firebase/Auth'
pod 'Firebase/Database'
pod 'Firebase/Storage'
pod 'FirebaseInstanceID', '2.0.0'
pod 'SDWebImage'
#pod 'Firebase/Firestone'
#pod 'Firebase/Messaging'
然后再次运行终端和 pod 安装。 最后,putData 完成处理程序被执行(甚至 ref.downloadURL 完成处理程序也被执行)
关于swift - putData 方法未被执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44729641/