Store Time efficiently in Firebase Database?

帅比萌擦擦* 提交于 2019-11-27 03:45:50

问题


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:


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)


来源:https://stackoverflow.com/questions/29905430/store-time-efficiently-in-firebase-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!