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.
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
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
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!)
}
}
}
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
The question is a bit vague but the first issue is using the event name as the documentID (key) to the event. While on the surface it seems like a good idea, it will end up being hard to maintain and difficult to change because documentID's (keys) cannot be changed. For example, If you name the event
and then a few days later you decide to change it to
You can't. You will have to read in the node, delete the existing node and re-write it out. Additionally, every single other place in your database that references that node will also have to be read in, deleted and read back out.
One flexible option is this structure
Now you can change the event name and will be much better for queries. It's often a good idea to disassociate documentID's from the data they contain.
Now to answer the question;
Firestore documents all have a documentID, that's whats used to uniquely identify one document from another. When reading in events, you want to keep track of that documentID within a class or structure, along with the rest of the fields.
For example - suppose we have events stored in our Firestore with the above structure and those are displayed in a tableView. You may have a class that stores each event as it's read from Firestore and a class var array that's used as a datasource for your tableView.
The general concept is when a user taps row 3 in the tableView, your app responds to that by reading the element in the dataSource array at row 3, which will be an EventClass. From there, get the documentID from that EventClass and add the attendee to 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.
2nd: Publish the data to firestore
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.
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