创建事件后将信息推送到Firebase(Firebase-Realtime-Database) [英] Pushing information to firebase after event is created(Firebase-Realtime-Database)

查看:86
本文介绍了创建事件后将信息推送到Firebase(Firebase-Realtime-Database)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个应用,用户可以在其中查看事件列表,确定他们要加入的事件,单击该特定事件,然后将其加入.我要解决的部分是,创建活动的人想要查看哪些人加入了该活动.我知道如何获取人们的电子邮件,但不知道如何在特定事件中将其发送到Firebase.

I am trying to create an app where the user can look at a list of events, decide which one they want to join, click on that specific event, and then join it. The part I am trying to solve is that the person who creates the event wants to see which people joined it. I know how to get the people's email, but dont know how to push it to firebase in that specific event.

这不使用Firestore

This is not using Firestore

有人创建了一个事件: 在此处输入图片描述

Someone creates an event: enter image description here

然后其他人在表格视图中看到该事件: 在此处输入图片描述

Then other people see that event in a table view: enter image description here

然后,用户可以单击事件以获取更多信息: 在此处输入图片描述

Then the user can click on an event to get more information: enter image description here

我现在想做的是当人们注册一个活动时,我想保存他们的电子邮件并将其发送到firebase,它将成为该活动的一部分: 在此处输入图片描述

What I want to do now is when people register for an event, I want to save their email and push it to firebase, where it will be part of this event: enter image description here

为进一步说明,这是我用来将事件详细信息推送到firebase的代码:

For further clarification, this is the code I use to push the events details to firebase:

@IBAction func registerEvent(_ sender: UIButton) {
    //push stuff to firebase
    let eventName = eventTitle.text!
    let eventDB = Database.database().reference().child("Events")
    let eventDict = ["EventTitle": eventTitle.text!, "numPeople": numberOfPeople.text!, "EventDescription": EventDescription.text!]
    
    eventDB.child(eventName).setValue(eventDict){
        (error, reference) in
        if(error != nil){
            print(error!)
        }
    }
    
}

我需要将注册事件的用户的电子邮件推送到Firebase数据库,但是此操作需要在其他ViewController中进行

I need to push the email of a user who registers for an event to the firebase database, but this action needs to take place in a different viewcontroller

如果需要更多说明,请告诉我

Please let me know if I need to clarify more

推荐答案

有两种不同的方法可以做到这一点,这实际上取决于您使用的是Realtime Database还是Firestore.以下信息将使用Firestore.

There are a couple of different ways to do this and it really depends on if you are using the Realtime Database or if you are using Firestore. The following information will be using Firestore.

第一个:配置数据库并创建要发布的事件的路径.

1st: Configure your database and create a path for your events to be posted.

// Create a path to the events DB
let eventsDB = Firestore.firestore().collection("events") 

// It is helpful to initialize your object with an id that way you can get it later
let eventID = eventsDB.document().documentID 

// Initialize your event with the eventID as an identifier 
let event = Event(id: eventID, etc.....)

2nd:将数据发布到firestore

2nd: Publish the data to firestore

// Turn your event into a dictionary 
// Your class or struct should have a data representation
let eventData = event.jsonRepresentation

// Create a path to prepare to publish to firestore
let path = Firestore.firestore().collection("users").document(id)

// Set the data
path.setData(value) { (error) in
     if let error = error {
          // There was an error handle it here
          return
     }
    // Anything passed this points means the data has been set
}

现在,当您获取并序列化数据时,您可以访问identifier属性并更新该特定文档.假设您的活动有参加者来存储其Firebase uid,那么您可以根据构建用户模型的方式引用其信息.

Now when you fetch and serialize your data you can access the identifier property and update that specific document. Assuming your event has attendees which stores their firebase uid than you can reference their information depending on how you build the User model.

第3步:更新活动,如果您的活动没有出席属性,请创建该活动.还可以在此处签出firebase交易.我不会在这里使用它,但这是一个很好的资源. https://firebase.google.com/docs/firestore/manage- data/transactions#transactions

Step 3: Update event, if your event doesn't have an attending property create it. Also checkout firebase transactions here. I won't be using that here but it is a good resource. https://firebase.google.com/docs/firestore/manage-data/transactions#transactions

// Append the new attending user to the old event 
let newEvent = event.attendees.append("some_unique_id_goes_here")

// Create a path
let path = firestore.firestore().collection("events").document(newEvent.identifer)

// Update the document at that path
dbPath.setData([attendingKey: newEvent.attendees], merge: true) { (error) in
    if let e = error {
        // There was an error handle it here
        return
    }
        // It was updated successfully 
}

这篇关于创建事件后将信息推送到Firebase(Firebase-Realtime-Database)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆