Sandor Ferreira Sandor Ferreira - 6 months ago 353
Swift Question

How to delete data from Firebase that is older than 6 hours in an iOS app?

I'm implementing an app for iOS that is based on events. These events have a startTime and endTime. They are available to the user to see only at this interval from startTime to endTime.
That's how it works: An user can create an event and post it to the Firebase database (the event contains startTime and endTime). So if the current time matches this interval, the user can see the current events that are going on; but if the endTime arrives, the event gets deleted from the database and the user can no longer access it.

The thing is I know nothing about javascript and how it could work on an iOS app with Firebase. I think (through my research) that I need something to check the database for old events and I have no idea how to implement it.

How does this check works on an iOS app with Firebase?
I'm sorry if I wasn't clear

Answer

I've had to implement something similar. Here's the breakdown of how you can achieve this with Firebase.

  1. Firebase does not provide server-side logic so you can't rely on Firebase deleting the data for you (in the case the event owner terminating the app, turning off phone, etc).

  2. Use FirebaseServerValue.timestamp to provide your event with a standard time value. This will be your constant to either allow or deny a user from seeing an event.

  3. Use if / else statements to control the event's visibility only between startTime and endTime (also set as timestamps).

  4. Because you can't use server side logic to delete the data, you must use the client to remove data from Firebase. Use the if / else logic from #3 to determine if your current timestamp value is past the endTime timestamp, and, if true, remove that piece of data from Firebase.

Hope this helps.

Comments