3

I was just wondering, is there a means to store Time in Firebase using Swift? The reason why I ask is because I want to make an application that when I create a particular object, the object will start a timer that counts down from 24 hours of when it was created. When those 24 hours expire, the object is deleted. I also want to show a count down timer to show how much longer the object has left before it's deleted.

I tried storing the hour and minutes as Integers into my database but that doesn't seem very efficient since I have to worry about AM/PM, and possibly have to worry about what day it is. I was also thinking about storing the date as a string but that seems arduous in figuring out how to constantly change the string to an integer and decrement the time to show the countdown that way.

I've looked into the FirebaseServerValue.timestamp() but I can't seem to store that into Firebase. Are there any tips or ideas on how one would implement this? Thanks.

EDIT: Attempt to store FirebaseServerValue.timestamp() into Firebase:

    self.firebaseRef.childByAutoId().setValue([
        "individualId":individualId.text,
        "timeCreated": FirebaseServerValue.timestamp()
        ])

However I get an error saying that '_' is not convertible to 'StringLiteralConvertible'. I tried to see if timestamp had any methods to turn it into a String or an Integer but couldn't find anything that I thought would be useful with the autocomplete.

user1871869
  • 3,317
  • 13
  • 56
  • 106
  • "I've looked into the `FirebaseServerValue.timestamp()` but I can't seem to store that into Firebase" Given that this feature is specifically meant to store timestamps, can you show your code? – Frank van Puffelen Apr 27 '15 at 20:41
  • @FrankvanPuffelen I showed how to store timestamps with Firebase in the original post. – user1871869 Apr 27 '15 at 20:48

1 Answers1

9

You can use time interval and just store it like a number

var interval = NSDate().timeIntervalSince1970

And when you need the date just get it like this

var date = NSDate(timeIntervalSince1970: interval)
Stefan Salatic
  • 4,513
  • 3
  • 22
  • 30
  • This works well, is there a way to store this as a string? Firebase seems to only take in strings.. – user1871869 Apr 27 '15 at 21:23
  • Just do `"\(interval)"` if you are using Swift, or string with format in Objective-C like `[NSString stringWithFormat:@"%f", interval];` – Stefan Salatic Apr 27 '15 at 21:26
  • I'm using Swift and when I try to do the `"\(interval)"` method it seems not to work. This is an example of how I'm trying to store it: `self.firebaseRef.childByAutoId().setValue([ "individualId":individualId.text, "timeCreated": "\(interval)" ])` But I get an error saying `Expected ',' seperator` – user1871869 Apr 27 '15 at 21:29
  • 9
    Firebase does not only take strings. That's absurd given that [the guide](https://www.firebase.com/docs/ios/guide/) uses numbers, booleans, and objects, and [the API](https://www.firebase.com/docs/ios/api/) contradicts this. Additionally, using NSDate depends on the date/time of the device, which may have drift/variance. Not as big of an issue on mobile, but a server timestamp will still be more reliable and consistent than NSDate. – Kato Apr 30 '15 at 17:23
  • Like @Kato has said, using the server timestamp is the way to go. Also for reference, never use timezones in server-side code. **Always UTC**. If you don't, [this happens](http://yellerapp.com/posts/2015-01-12-the-worst-server-setup-you-can-make.html). – swiftcode May 09 '16 at 19:40