How to use an IBAction to write data to Firebase

110 Views Asked by At

I'm having trouble figuring out how to write data to my Firebase Database through an IBAction.

My problem is not being able to write to the actual database, it's the type of data that is being written.

I need to write data using a Google Maps Services Place object. Here's my database write code:

let newWaitTime = Int(waitTimeLabel.text!)

let waitTime = databaseRef.child("times").child(place.placeID).childByAutoId()

waitTime.setValue(newWaitTime)

The problem is since IBAction functions aren't able to take other parameters, I can't pass my place variable into the function in order to allow me to write to my database.

I can't seem to figure out a workaround because it requires knowing the place.placeID to submit the times. Without that there is no way to distinguish which times belong to which places. Any thoughts?

1

There are 1 best solutions below

2
Justin Doan On BEST ANSWER

in the main class add the following

var currentPlace: Place?

then whenever you are interacting with a certain place

self.currentPlace = placeImInteractingWith

in the IBAction

if let placeToUse = currentPlace {
    let waitTime = databaseRef.child("times").child(placeToUse.placeID).childByAutoId()
    //etc
{