简体   繁体   English

Swift Firebase downloadURL返回nil

[英]Swift Firebase downloadURL returns nil

I have updated my firebase code and I have got my data to save to the database and save to the storage, yet my downloadURL returns nil. 我已经更新了我的firebase代码,已经将数据保存到数据库并保存到存储,但是我的downloadURL返回nil。 I am not sure why my downloadURL would return nil since everything is saving correctly. 我不确定为什么我的downloadURL返回零,因为一切都正确保存了。 Could be so obvious I am overlooking it... it happens lol 可能是如此明显,我忽略了它...发生大声笑

let photosRef = storage.reference().child("posts").child((loggedInUser?.uid)!)
    let usersRef = Database.database().reference().child("Businesses")
    let databaseRef = Database.database().reference()
    let imageName = NSUUID().uuidString
    let photoRef = photosRef.child("\(uid)")
    let postID = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId().key
    photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
        if let error = error {
            print("there was an error")
            print(error.localizedDescription)
            return
        } else {
            // store downloadURL
            photoRef.downloadURL(completion: {(url, error) in
                if error != nil {

                    let downloadURL = url?.absoluteString
                    print(downloadURL)
                    print("HERE#####################################")
                    let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "download_url": downloadURL, "timestamp": ServerValue.timestamp(), "businessName":loggedInUserData?["businessName"] as! String, "businessStreet":loggedInUserData?["businessStreet"] as! String, "businessCity":loggedInUserData?["businessCity"] as! String, "businessState":loggedInUserData?["businessState"] as! String, "businessZIP":loggedInUserData?["businessZIP"] as! String, "businessPhone":loggedInUserData?["businessPhone"] as! String, "businessWebsite":loggedInUserData?["businessWebsite"] as! String, "businessLatitude":loggedInUserData?["businessLatitude"] as! String, "businessLongitude":loggedInUserData?["businessLongitude"] as! String, "facebookURL":loggedInUserData?["facebookURL"] as! String, "twitterURL":loggedInUserData?["twitterURL"] as! String, "instagramURL":loggedInUserData?["instagramURL"] as! String, "googleURL":loggedInUserData?["googleURL"] as! String, "yelpURL":loggedInUserData?["yelpURL"] as! String, "foursquareURL":loggedInUserData?["foursquareURL"] as! String, "snapchatURL":loggedInUserData?["snapchatURL"] as! String, "imageID": imageName, "postID": postID]

                    // store downloadURL at database
                    let databaseRef = Database.database().reference()
                    let path = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId()
                    path.setValue(values) { (error, ref) -> Void in
                        if error != nil {
                            print("error saving post in db")
                        } else {
                            // reset caption field
                            self.descriptionTextView.text = ""
                            // reset placeholder image
                            self.imageView.image = UIImage(named: "filterPlaceholder")
                            MBProgressHUD.hide(for: self.view, animated: true)
                            let viewConrolller = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile") as! UITabBarController
                            self.present(viewConrolller, animated: true, completion: nil)
                        }
                    }
                } else {
                    print(error!.localizedDescription)
                    print("error")
                    return
                }
            })
        }
    }

Easy Mistake 容易犯错

I was not accessing the actual image which is why it was returning nil 我没有访问实际的图像,这就是为什么它返回nil

 photoRef.child("\(imageName)").downloadURL(completion: { (url, error) in

Your if statement is flawed in the completion handler of photoRef.downloadURL , you try using downloadURL in case error had a value, meaning that there was a networking error. 您的if语句在photoRef.downloadURL的完成处理程序中存在缺陷,请尝试使用downloadURL ,以防error具有值,这意味着存在网络错误。

photoRef.downloadURL(completion: {(url, error) in
    if let downloadURL = url, error == nil {...

The ref.downloadURL completion handler will not be executed until do this trick: 除非执行以下操作,否则不会执行ref.downloadURL完成处理程序:

Just fix 'FirebaseInstanceID' to version '2.0.0' in the podfile, putData method will be executed. 只需将podfile中的“ FirebaseInstanceID”修复为版本“ 2.0.0”,将执行putData方法。 It look like this in podfile: pod 'FirebaseInstanceID', '2.0.0' 在podfile中看起来像这样:pod'FirebaseInstanceID','2.0.0'

I'm using Xcode 10.2 and Swift 5, the trick still works 我正在使用Xcode 10.2和Swift 5,该技巧仍然有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM