Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法在我的Firebase实时数据库中存储URL字符串_Ios_Swift_Firebase_Firebase Realtime Database_Firebase Authentication - Fatal编程技术网

Ios 无法在我的Firebase实时数据库中存储URL字符串

Ios 无法在我的Firebase实时数据库中存储URL字符串,ios,swift,firebase,firebase-realtime-database,firebase-authentication,Ios,Swift,Firebase,Firebase Realtime Database,Firebase Authentication,我在ViewController中设置了URLstring变量,但当我尝试在实时数据库中设置“profileImageURL”的值时,它不会显示所需的URL,即使从storageRef中提取它时,它也会很好地打印出来 我认为这是因为我在方法中声明了URLString变量(我在本例中是一个按钮),但即使现在我在ViewController中声明了它,它也不起作用 代码如下: Auth.auth().createUser(withEmail: emailTextField.text!, passwo

我在ViewController中设置了URLstring变量,但当我尝试在实时数据库中设置“profileImageURL”的值时,它不会显示所需的URL,即使从storageRef中提取它时,它也会很好地打印出来

我认为这是因为我在方法中声明了URLString变量(我在本例中是一个按钮),但即使现在我在ViewController中声明了它,它也不起作用

代码如下:

Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (user, error) in

        if error != nil{
            print(error!.localizedDescription)
            return
        }

        let uid = Auth.auth().currentUser?.uid
        let storageRef = Storage.storage().reference(forURL: "gs://instagramclone-1ed36.appspot.com").child("Profile_Images").child(uid!)
        let ref = Database.database().reference()

        if let profileIMG = self.selectedImage, let imageData = profileIMG.jpegData(compressionQuality: 0.1){
            storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in
                if error != nil {
                    return
                }


                storageRef.downloadURL(completion: { (url, error) in
                    if error != nil {
                        return
                    }else {
                        self.URLString = url!.absoluteString
                        print("\n\n the URLString is : \(self.URLString)\n\n")
                    }

                })

                let userReference = ref.child("users")
                let newUserReference = userReference.child(uid!)
                newUserReference.setValue([ "profileImageURL": self.URLString])

            })
        }
    }

问题在于,此代码
newUserReference.setValue([“profileImageURL”:self.URLString])
将在storageRef闭包中的代码
self.URLString=url!之前运行!。绝对字符串
。Firebase是异步的,代码比internet快。因此,url仅在storageRef闭包内有效-之后的任何代码都将在之前运行。
// Fetch the download URL
            storageRef.downloadURL { url, error in
                if let error = error {
                    // Handle any errors
                    if(error != nil){
                        print(error)
                        return
                    }
                } else {
                    // Get the download URL for 'images/stars.jpg'
                    let urlStr:String = (url?.absoluteString) ?? ""
                    let values = ["imageUrl": urlStr,"date_time": self.getDate()]
                    self.registerUserIntoDatabaseWithUID(uid: userID, values: values as [String : AnyObject])
                }
            }


func registerUserIntoDatabaseWithUID(uid:String, values:[String:AnyObject]){
    let ref = Database.database().reference(fromURL: "https://yourpath.firebaseio.com/")
    let usersReference = ref.child("abc").child(uid).child("\(self.yourID!)")

    usersReference.updateChildValues(values) { (error, ref) in
        if(error != nil){
            print(error)
            return
        }
        self.dismiss(animated: true, completion: nil)
    }
}