【问题标题】:Pushing information to firebase after event is created(Firebase-Realtime-Database)创建事件后将信息推送到 Firebase(Firebase-Realtime-Database)
【发布时间】:2019-06-18 20:55:59
【问题描述】:

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

这不是使用 Firestore

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

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

然后用户可以点击一个事件来获取更多信息: enter image description here

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

为了进一步说明,这是我用来将事件详细信息推送到 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 数据库,但此操作需要在不同的视图控制器中进行

如果我需要进一步说明,请告诉我

【问题讨论】:

  • 显然我不明白这个问题。部分问题是创建活动的人想查看哪些人加入了它,但后来我想保存他们的电子邮件并将其推送到firebase。这是两个不同的东西。您是在问如何在 firebase 中保存电子邮件,或者一般如何将数据写入 firebase,或者当有人加入活动时如何通知原始创建者。或者……完全是别的东西?您能否将您尝试过的代码包含在内,并告诉我们哪些代码不起作用?
  • 我刚刚添加了一个更新来澄清
  • 我对此有所了解。这是一个非常广泛的问题,有很多解决方案,所以我不得不对代码的结构做出一些假设。

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

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

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

// 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.....)

第二次:将数据发布到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
}

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

第 3 步:更新活动,如果您的活动没有参加的财产,请创建它。还可以在这里查看 firebase 交易。我不会在这里使用它,但它是一个很好的资源。 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 
}

【讨论】:

  • 这如何为 OP 的问题提供解决方案创建活动的人希望查看哪些人加入了它?这似乎是有关如何将数据写入 Firebase 的信息,但没有关于该问题的信息。你能澄清一下答案吗?
  • 问题是如何将更新推送到特定事件。为此,他需要在第一次创建 eventID 时保存它。
【解决方案2】:

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

Forth Of July Big Bash

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

Forth Of July Big Bash 2019

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

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

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 数组。

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 行时,您的应用会通过读取 dataSource 数组中第 3 行的元素来响应,这将是一个 EventClass。从那里,从该 EventClass 中获取 documentID 并将参加者添加到 Firestore。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-10
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    相关资源
    最近更新 更多