Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
Swift 写入Firebase中的多个节点?_Swift_Firebase_Firebase Realtime Database - Fatal编程技术网

Swift 写入Firebase中的多个节点?

Swift 写入Firebase中的多个节点?,swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,一旦用户点击“post”按钮,就会发生两件事: 将创建一个“注释”节点 将创建一个“消息”节点 然而,我似乎无法让它写入firebase两次: let ref = Database.database().reference() let userProfile = UserService.currentUserProfile let key = ref.child("post").childByAutoId().key let feed = ["comment": commentView.t

一旦用户点击“post”按钮,就会发生两件事:

  • 将创建一个“注释”节点
  • 将创建一个“消息”节点
  • 然而,我似乎无法让它写入firebase两次:

    
    let ref = Database.database().reference()
    let userProfile = UserService.currentUserProfile
    
    let key = ref.child("post").childByAutoId().key
    
    let feed = ["comment": commentView.text,
                "username": uid,
                "timestamp": [".sv":"timestamp"],
                "author": [
                    "cid": userProfile?.uid,
                    "username": userProfile?.username,
                    "fullname": userProfile?.fullname,
                    "patthToImage": userProfile?.patthToImage.absoluteString]] as [String : Any]
    
    let commentFeed = ["\(key!)" : feed]
    let commentorFeed = [uid: uid]
    let commentFe = ["messages": feed]
    
    // my comment node, which is working         
    
    ref.child("posts").child(myPost).child("comment").updateChildValues(commentFeed)
    ref.child("posts").child(myPost).updateChildValues(commentorFeed)
    
    //my message node which isn't working
    
    let ches = Database.database().reference().child("messages")
    ches.setValue("hahah")
    

    这可能吗?

    这不是两次保存值的问题。这似乎是此代码的一个问题:

    let ches = Database.database().reference().child("messages")
    ches.setValue("hahah")
    
    您需要改为使用
    push()


    在Swift中,将是代码> Re.Far子(“消息”).HythByAutoID()。StValuy(“HHA”)。但是,我不会考虑用一个新的唯一ID创建一个孩子,而是重新使用已经为该帖子生成的密钥。所以:
    ref.child(“messages”).child(key).setValue(“hahah”)
    ref.child("messages").push("hahah")