Store Time efficiently in Firebase Database?

2019-01-24 19:45发布

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.

1条回答
Animai°情兽
2楼-- · 2019-01-24 20:17

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)
查看更多
登录 后发表回答