swift - putData 方法未被执行

标签 swift firebase-realtime-database save firebase-storage imageurl

我正在尝试实现更新用户个人资料图片的功能,这反过来将更新(替换)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/

相关文章:

ios - 带有 SwiftUI + 拖放重新排序的 UICollectionView 可能吗?

ios - AWS DynamoDB 在 LIST 或 NUMBER SET 中插入值

swift - SceneKit 着色器修改器

控制台中的 Firebase 实时数据库无法正常工作?

javascript - 如何在 HTML 中显示 Firebase 查询结果(未定义错误)

java - 在 Java 中序列化并保存 double [] 的 ArrayList

go - Golang Gorilla session 无法在Windows10上保存

Swift:绘图开始时图像丢失纵横比

java - 如何从监听器获取数据?

iphone - IOS:应用程序关闭或进入后台时保存数组