在同一UID Firebase Swift下保存数据

发布于 2025-02-13 02:10:32 字数 1469 浏览 0 评论 0原文

我已经在Firebase实时数据库中添加了一些数据。它分为两个部分,用户和预订。我的问题是,当用户预订乘车时,它会提供不同的UID,我希望当用户书本骑行时,它处于同一UID之下。另外,如果开关打开,我想将其放在同一子节点下,预订,但是保存时它会变成不同的节点。任何帮助将不胜感激。

   func Save(){

    let key = ref.childByAutoId().key
    
    let Booking = [
    
        
        "id": key as Any,
        "What Date": atALaterDate.text! as String,
        "What Time": hoursMinutesTextField.text! as String,
        "To What Time": ToWhatTime.text! as String,
        "Requests or Notes": SRtextfield.text! as String

    ]
    
    as [String : Any]
        
    ref.child(key!).setValue(Booking)


    }

在这里,我希望它与上面的节点结合使用。

func Save2(){

    let key = ref.childByAutoId().key
    
        if Switch.isOn{
            
            let passengers = [
            "Passengers": Stepperlabel.text! as String,
            "passenger Age 1": AgeOfChild1.text! as String,
            "passenger Age 2": AgeOfChild2.text! as String,
            "passenger Age 3": AgeOfChild3.text! as String,
            "passenger Age 4": AgeOfChild4.text! as String,
            "passenger Age 5": AgeOfChild5.text! as String,
            "passenger Age 6": AgeOfChild6.text! as String,
      
                ]
            
            as [String : Any]
                
            ref.child(key!).setValue("booking")
                
                
                
        }else{
            print("Hello")
                return
        }
        
    }

I have added some data to the Firebase Realtime Database. It's in two parts, users and bookings. My problem is that when a user books a ride it gives a different uid, I would like it to be when a user books a ride it is under the same uid. Also, if the switch is on I would like to put it under the same child node, booking, but it comes into different nodes when saving. Any help would be highly appreciated.

   func Save(){

    let key = ref.childByAutoId().key
    
    let Booking = [
    
        
        "id": key as Any,
        "What Date": atALaterDate.text! as String,
        "What Time": hoursMinutesTextField.text! as String,
        "To What Time": ToWhatTime.text! as String,
        "Requests or Notes": SRtextfield.text! as String

    ]
    
    as [String : Any]
        
    ref.child(key!).setValue(Booking)


    }

this is where I would like it to be under one node, in conjunction with the one above it.

func Save2(){

    let key = ref.childByAutoId().key
    
        if Switch.isOn{
            
            let passengers = [
            "Passengers": Stepperlabel.text! as String,
            "passenger Age 1": AgeOfChild1.text! as String,
            "passenger Age 2": AgeOfChild2.text! as String,
            "passenger Age 3": AgeOfChild3.text! as String,
            "passenger Age 4": AgeOfChild4.text! as String,
            "passenger Age 5": AgeOfChild5.text! as String,
            "passenger Age 6": AgeOfChild6.text! as String,
      
                ]
            
            as [String : Any]
                
            ref.child(key!).setValue("booking")
                
                
                
        }else{
            print("Hello")
                return
        }
        
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任谁 2025-02-20 02:10:32

我相信问题是如何将其他数据添加到节点。

重要的一点是将要写入的节点保留并使用update来添加/更改现有数据的节点。如果更新在不存在的节点上使用,则将添加。

所以这是一个例子。假设我们有一个聊天应用程序,我们想编写一个 hello,world 的初始字符串,然后添加一个的额外字符串,该是什么

let messagesRef = self.ref.child("messages") //the top level node
let thisMsgRef = ref.childByAutoId() //create a child node
        
thisMsgRef.setValue( ["msg": "Hello, World"]) //create the node, write data
thisMsgRef.updateChildValues(["anotherMsg": "Whats up?"]) //add addl data

?以下结构

messages
   -Jy444f476u6...
      anotherMsg: "Whats up?"
      msg: "Hello, World"

如果要一遍又一遍地重新使用节点,则可以将其引用作为var存储,然后在任何时候写入它,只需引用var ...

class ViewController: NSViewController {  
    var myMsgRef: DatabaseReference!

然后稍后在代码中的某个地方

func writeInitialData() {
   let ref = self.ref.child("messages")
   self.myMsgRef = ref.childByAutoId()
   self.myMsgRef.setValue( ["msg": "Hello World"])

,然后当您要添加其他数据时

func addAdditonalData() {
   self.myMsgRef.updateChildValues(["anotherMsg": "Whats up?"])

I believe the question is how to add additional data to a node.

The important bit is to keep a reference to the node you want to write to and use it with update to add/change existing data. If update is used on a node that doesn't exist, it will be added.

So here's an example. Suppose we have a chatting app and we want to write an initial String of Hello, World, and then add an additional string of What's up? to that same node

let messagesRef = self.ref.child("messages") //the top level node
let thisMsgRef = ref.childByAutoId() //create a child node
        
thisMsgRef.setValue( ["msg": "Hello, World"]) //create the node, write data
thisMsgRef.updateChildValues(["anotherMsg": "Whats up?"]) //add addl data

this results in the following structure

messages
   -Jy444f476u6...
      anotherMsg: "Whats up?"
      msg: "Hello, World"

If you want to re-use the node over and over, a reference to it could be stored as class var and then anytime you want to write to it, just reference the var...

class ViewController: NSViewController {  
    var myMsgRef: DatabaseReference!

then somewhere later in code

func writeInitialData() {
   let ref = self.ref.child("messages")
   self.myMsgRef = ref.childByAutoId()
   self.myMsgRef.setValue( ["msg": "Hello World"])

and then when you want to add additional data

func addAdditonalData() {
   self.myMsgRef.updateChildValues(["anotherMsg": "Whats up?"])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文