首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建事件后向firebase推送信息(Firebase-Realtime-Database)

创建事件后向firebase推送信息(Firebase-Realtime-Database)
EN

Stack Overflow用户
提问于 2019-06-19 04:55:59
回答 2查看 350关注 0票数 0

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

这不是使用Firestore

有人创建了一个事件:enter image description here

然后其他人在表视图中看到该事件:enter image description here

然后,用户可以单击某个事件以获取更多信息:enter image description here

我现在想做的是,当人们注册一个活动时,我想保存他们的电子邮件并将其推送到firebase,在那里它将成为此活动的一部分:enter image description here

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

代码语言:javascript
复制
@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数据库,但此操作需要在不同的视图控制器中进行

如果我需要更多的澄清,请告诉我

EN

回答 2

Stack Overflow用户

发布于 2019-06-19 07:03:44

有几种不同的方法可以做到这一点,这真的取决于你是使用实时数据库还是使用Firestore。以下信息将使用Firestore。

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

代码语言:javascript
复制
// 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.....)

第2步:将数据发布到firestore

代码语言:javascript
复制
// 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
}

现在,在获取和序列化数据时,可以访问标识符属性并更新该特定文档。假设您的活动有参与者存储了他们的firebase uid,那么您可以引用他们的信息,这取决于您如何构建用户模型。

步骤3:更新事件,如果您的事件没有出席属性,请创建它。也可以在这里签出firebase事务。我不会在这里使用它,但它是一个很好的资源。https://firebase.google.com/docs/firestore/manage-data/transactions#transactions

代码语言:javascript
复制
// 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 
}
票数 3
EN

Stack Overflow用户

发布于 2019-06-20 23:09:36

这个问题有点模糊,但第一个问题是使用事件名称作为事件的关键字( documentID )。虽然从表面上看这似乎是一个好主意,但最终它将很难维护和更改,因为documentID(键)不能更改。例如,如果将事件命名为

代码语言:javascript
复制
Forth Of July Big Bash

几天后,您决定将其更改为

代码语言:javascript
复制
Forth Of July Big Bash 2019

你不能。你必须读入节点,删除现有的节点,然后重写它。此外,数据库中引用该节点的所有其他位置也必须被读入、删除和回读。

一种灵活的选择是这种结构

代码语言:javascript
复制
Events //collection
   document_0 //a document within the collection
      event_name: "My Event"
      num_people: 10
      event_date: "20190704"
      registration //a collection
           doc_0:
               email: some email
           doc_1:
               email: another email

现在,您可以更改事件名称,这样可以更好地进行查询。将documentID从它们所包含的数据中分离出来通常是一个好主意。

现在回答问题;

Firestore文档都有一个documentID,它用来唯一地区分文档。在读入事件时,您希望跟踪类或结构中的documentID,以及其他字段。

例如,假设我们在Firestore中存储了具有上述结构的事件,并在tableView中显示这些事件。您可能有一个存储从Firestore读取的每个事件的类,以及一个用作tableView数据源的类var数组。

代码语言:javascript
复制
class EventClass { //this is the class that holds the events for the array
   var eventDocumentId = ""
   var eventName = ""
   var eventDate = ""
   var numPeople = ""
}

class ViewController: NSViewController {
   var myEventArray = [EventClass]()

   func createEvent() {
        let eventCollection = self.db.collection("Events")
        let eventName = "July 4th Big Bash"
        let eventDate = "20190704"
        let numPeople = "10"

        let eventDict = [
            "event_name": eventName,
            "event_date": eventDate,
            "num_people": numPeople
        ]

        //if there's an observer for the Event collection, this would fire
        //  passing in this event so an EventClass could be created
        //  and then added to the tableView dataSource.
        eventCollection.addDocument(data: eventDict)
   }

   func readEvents() {
       //Read events and populate the myEventArray with EventClasses
       //As each event is read in, create an EventClass and populate it
       // from the Firestore fields and also it's .documentID
   }

   func addAttendee() {
     let email = "test@thing.com" //attendee email address
     let docId = "cP3lXY5htLRqMGDZckv5" //get the documentID of the tapped event
     let eventCollection = self.db.collection("Events")
     let eventRef = eventCollection.document(docId)
     let registrationRef = eventRef.collection("registration")
     let dict = [
         "email":email
     ]

     registrationRef.addDocument(data: dict)
   }

一般的概念是,当用户点击tableView中的第3行时,您的应用程序通过读取第3行的dataSource数组中的元素进行响应,该元素将是一个EventClass。从那里,从该EventClass获取documentID,并将与会者添加到Firestore。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56656922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档