简体   繁体   English

Swift Firebase 数据库覆盖

[英]Swift Firebase database overwriting

I am making a real-time messenger using Firebase. Currently, whenever I press a button I want a new message to be appended to the channel with the index of the message, but currently, whenever I press the button a new message is created that overwrites the old message.我正在使用 Firebase 制作实时信使。目前,每当我按下一个按钮时,我希望将一条新消息附加到带有消息索引的频道,但目前,每当我按下按钮时,都会创建一条新消息覆盖旧消息。 I know that setValue is usually the issue, but I really cannot tell what I'm doing wrong.我知道 setValue 通常是问题所在,但我真的不知道我做错了什么。 What the database looks like before I add my new message .添加新消息之前数据库的样子 This is what it looks like after I add a new message here , and then the code I am using to add to the database.这是我在此处添加新消息后的样子,然后是我用来添加到数据库的代码。

@IBAction func sendMessageTapped(_ sender: Any) {
    if messageTextField.text == "" {
        print("blank")
        return
    } else {
        // First we will update the amount of messages that the channel has.
        ref.child("channels").child(channelName!).setValue(["numberOfMessages" : numberOfMessages+1 ])
        numberOfMessages += 1
        // after we have updated the amount of messages we will try to create a new message.
        ref.child("channels").child(channelName!).child("messages").child(String(numberOfMessages)).child("message").child("content").setValue(messageTextField.text)
        ref.child("channels").child(channelName!).child("messages").child(String(numberOfMessages)).child("message").child("name").setValue("Buddy")

    }
}

ok, Firebase is not a traditional table based database, is a DOCUMENT based database. 好的,Firebase不是传统的基于表的数据库,而是基于DOCUMENT的数据库。 At the very top you have a thing called a "collection" which is just a list of "document" things. 在最顶部,您有一个叫做“集合”的东西,它只是“文档”东西的列表。 In your case, you'd have several collection things to serve as channels: "General", "TopicQ", "InterstingStuff" etc, and within them each message as a document. 在您的情况下,您将有几个集合内容可以用作渠道:“常规”,“ TopicQ”,“ InterstingStuff”等,并且其中每个消息都作为文档。 No need to have a document, to then list the messages within it. 不需要文档,然后列出其中的消息。

Second, you don't need indexes as you're using them, make the message id an attribute of the message, because firebase support querying by field, and even then is questionable because if you make each message a document, they will have their own auto generated id's if you want. 其次,您在使用索引时不需要索引,将消息ID设置为消息的属性,因为Firebase支持按字段查询,即使这样,还是有问题的,因为如果将每条消息都设为文档,它们将具有如果需要,可以使用自己的自动生成的ID。

Third, in your code you're rewriting the whole document each time, this is why you lose your previous messages, so if you keep it, you need to add a merge option: 第三,在您的代码中每次都重写整个文档,这就是为什么您丢失以前的消息的原因,因此,如果保留它,则需要添加合并选项:

// Update one field, creating the document if it does not exist.
db.collection("cities").document("BJ").setData([ "capital": true ], merge: true)

you probably want to do something like this.你可能想做这样的事情。 This is what I did for my app, hope this helps someone.这就是我为我的应用程序所做的,希望这对某人有所帮助。 This rootRef.childByAutoId() generates a new entry with unique id.此 rootRef.childByAutoId() 生成一个具有唯一 ID 的新条目。 You can use this as reference for your case.您可以将其用作您的案例的参考。

 let rootRef = Database.database().reference(withPath: "channels")
 let childRef = rootRef.childByAutoId()
 let values = ["Type": self.textField.text!, "message": self.textView.text!] as? [String : Any]
 childRef.updateChildValues(values)

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

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