【问题标题】:Swift Firebase save downloadURL stringSwift Firebase 保存 downloadURL 字符串
【发布时间】:2018-10-02 22:01:15
【问题描述】:

我更新了我的 firebase 代码,因为他们将“metaData!.downloadURL()!.absoluteString”更改为我将在下面发布的代码。我的问题是我需要将 downloadURL 字符串与帖子的其余信息一起保存。由于它是一种异步方法,因此我无法在调用之外访问字符串,并且当我尝试将“.setValue”放入调用中时,它根本不起作用。这是我之前的问题,但没有关于这个问题的答案:Swift Firebase metaData!.downloadURL()!.absoluteString

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
    var downloadURLSting = String()

    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


            storage.reference().downloadURL(completion: {(url, error) in
                if error != nil {
                    print(error!.localizedDescription)
                    return
                }
                let downloadURL = url?.absoluteString

            })

            let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "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)
                }
            }
        }
    }

安全规则

service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
  allow read, write: if request.auth != null;
}
}
}

此代码有效,只是“downloadURL”返回 nil

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 {

                    guard let downloadURL = url?.absoluteString else { return }

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

【问题讨论】:

  • 在将 setValue 放在 downloadUrl 回调中时,您能否定义“不起作用”?你有错误吗?
  • 当所有信息都可以写入时,尝试完全在 downloadURL 的完成内写入数据库。
  • 当我这样做时,我得到“用户无权访问 gs://shoppeer-e7270.appspot.com/(null)。”
  • @DougStevenson 我如何在“.putData(data!, metadata: nil) { (metaData,error) in”之外访问“downloadURL” 我试过把我的“Database.setValue”放在里面并收到错误“用户无权访问 gs://shoppeer-e7270.appspot.com/(null)。”
  • 现在你遇到了一个完全不同的问题,可能是安全规则。

标签: ios swift firebase firebase-storage


【解决方案1】:

这可能是因为您的变量不在同一范围内。当您尝试这样做时会发生什么?

photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
    if let error = error {
        print("there was an error")
        print(error.localizedDescription)
        return
    } 

    storage.reference().downloadURL(completion: {(url, error) in
            if error != nil {
                print(error!.localizedDescription)
                return
            }
            let downloadURL = url?.absoluteString

            let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "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]

        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 viewController = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile") as! UITabBarController
                self.present(viewController, animated: true, completion: nil)
            }
        }

    })        
}

【讨论】:

  • 我收到“用户无权访问 gs://shoppeer-e7270.appspot.com/(null)。”
猜你喜欢
  • 2019-01-25
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 2016-11-02
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 2019-03-31
相关资源
最近更新 更多