【问题标题】:Firebase Loading to Different Nodes iOS SwiftFirebase 加载到不同的节点 iOS Swift
【发布时间】:2017-07-20 05:57:30
【问题描述】:

我正在尝试将我的描述、位置和图片加载到同一个 AutoId 到 firebase。我必须先存储 Pic,然后获取 URL,然后将 URL 添加到 Pic 值。我尝试先上传描述和位置,将图片留空,然后返回并添加图片 URL,但我的 autoID 键值似乎不起作用。

它不断将 Pic URL 放入不同的 childAutoId。如何让它们存储到同一个子节点中?

创建 AutoID 节点的初始函数

    @IBAction func postToWall(_ sender: Any) {
    let location : String = locationTextField.text!
    let description : String = descriptionTextField.text!


    self.ref.child("Posts").childByAutoId().setValue(["Location" : location, "Description" : description, "Pic" : ""])

    let newRef = ref.child("Posts").childByAutoId()
    print(newRef)
    key = newRef.key
    print(key)
    savePicPost()
}

将图片保存到存储后,尝试返回刚刚创建的 ID 并保存图片 URL

    func savePicPost(){
    //Store the image in storage
    let imageName = NSUUID().uuidString
    let storedImage = storageRef.child("PostPics/").child(imageName)

    if let uploadData = UIImagePNGRepresentation(self.postImage.image!) {
        storedImage.putData(uploadData, metadata: nil, completion: { (metadata, error) in

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

            if let picPostURL = metadata?.downloadURL()?.absoluteString {
                self.ref.child("Posts").child(self.key).updateChildValues(["Pic" : picPostURL], withCompletionBlock: { (error, ref) in

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

                })
            }

        })
    }
}

这是保存到不同 ID 的方式 Firebase Screenshot

我很近,我能感觉到。尽管我认为某些东西必须符合我的关键价值。任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: ios swift xcode firebase


    【解决方案1】:

    简单明了。您在 key 中存储了一个全新的孩子,而不是您设置值时使用的那个。

    // This creates a new child
    let newRef = ref.child("Posts").childByAutoId()
    

    相反,请继续保留对您的 childByAutoId 的引用并将其传递给您的 savePicPost()。在那里您可以使用它来存储您的图片 URL。即

    let post = self.ref.child("Posts").childByAutoId()
    post.setValue(["Location" : location, "Description" : description, "Pic" : ""])
    
    // call your method like so
    func savePicPost(into: post)
    
    // use this ref to update picture URL
    post.updateChildValues(["Pic" : picPostURL])
    

    希望对你有帮助。

    【讨论】:

    • 嗯,好的。我不太了解它,但让我玩弄它并尝试弄清楚。感谢您的回复
    • childByAutoId() 基本上返回FirebaseDatabaseReference 并且由于您想稍后重用它,您可以保留对它的引用并稍后在您的savePicPost() 中使用它。我没有任何 Firebase 项目设置 atm,因此无法发布完整的确切代码。
    【解决方案2】:

    是的,我现在看到了。谢谢你。我正在创建一个新 ID 而不是使用旧 ID。这是我的代码现在的样子,我注释掉了我删除的内容。我实际上删除了一些以使其正常工作。有趣的是这是怎么回事。谢谢

    @IBAction func postToWall(_ sender: Any) {
        let location : String = locationTextField.text!
        let description : String = descriptionTextField.text!
    

    // 让 newRef = ref.child("Posts").childByAutoId() 让 newRef = self.ref.child("Posts").childByAutoId()

    // self.ref.child("Posts").childByAutoId().setValue(["Location" : location, "Description" : description, "Pic" : ""])

        newRef.setValue(["Location" : location, "Description" : description, "Pic" : ""])
    
    
        print(newRef)
        key = newRef.key
        print(key)
        savePicPost()
    

    // 打印(键)

    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 2023-03-25
      • 2016-12-15
      • 2017-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多