Ios Swift Firebase downloadURL返回零

Ios Swift Firebase downloadURL返回零,ios,swift,firebase,firebase-storage,Ios,Swift,Firebase,Firebase Storage,我已经更新了我的firebase代码,我已经将数据保存到数据库并保存到存储器,但我的下载URL返回零。我不知道为什么我的下载URL会返回零,因为一切都保存正确。很明显我忽略了它。。。发生了,哈哈 let photosRef = storage.reference().child("posts").child((loggedInUser?.uid)!) let usersRef = Database.database().reference().child("Businesses")

我已经更新了我的firebase代码,我已经将数据保存到数据库并保存到存储器,但我的下载URL返回零。我不知道为什么我的下载URL会返回零,因为一切都保存正确。很明显我忽略了它。。。发生了,哈哈

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
                }
            })
        }
    }

您的if语句在
photoRef.downloadURL
的完成处理程序中有缺陷,如果
error
有值,则尝试使用
downloadURL
,这意味着出现了网络错误

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

在执行以下操作之前,将不会执行ref.downloadURL完成处理程序:

只需在podfile中将“FirebaseInstanceID”修复为版本“2.0.0”,就会执行putData方法。它在podfile中看起来像这样: pod“FirebaseInstanceID”,“2.0.0”


我使用的是Xcode 10.2和Swift 5,技巧仍然有效

使用这段代码让我退了一步,因为现在它说firebase中的路径不存在。当我返回到上面的代码时,所有内容都正确保存,但下载URL仍然为零“Object posts/WQYMJTS438XSJWQ13Ciayzs9AM2/WQYMJTS438XSJWQ13Ciayzs9AM2不存在。”这是我使用code@LukasBimba如果这句话绝对不正确,花点时间好好想想你在做什么。它试图处理
下载URL
,以防
出错!=nil
,这意味着网络请求失败,因此
downloadURL
只能是
nil
。如果你看到更多的问题,这意味着你的逻辑中还有其他缺陷,我也会调查这些问题。我正在使用你提供给我的代码。我很感激你的帮助我已经弄明白了,我只是没有看到真实的图像
photoRef.downloadURL(completion: {(url, error) in
    if let downloadURL = url, error == nil {...