[英]What is the best way to deal with library dependencies in android if you are working as a team?
[英]What is the best way to deal with Date in iOS / Android and Firebase?
我正在android和iOS(swift3)中開發兩個本機應用程序,我的某些類使用Date對象,但是當我將android中的Date對象保存到Firebase Realtime Database中時,結構如下所示:
creationDate: { date: 3 day: 2 hours: 17 minutes: 27 month: 9 seconds: 43 time: 1507062463000 timezoneOffset: 180 year: 117 }
但這不是iOS中的常見結構(根據我的搜索當然)。
有什么更好的解決方案:
謝謝
我建議使用FIRServerValue.timestamp()
。 這樣可以確保將數據保存到數據庫時使用Firebase Server時間戳,這是最准確的時間,因為它不能被用戶篡改,並且在iOS和Android上完全相同。
保存的數據是自1970年以來的毫秒數,您可以輕松地將其轉換為日期對象。
例如(Swift):
myRef.setValue(FIRServerValue.timestamp())
編輯:Firebase 4
myRef.setValue(ServerValue.timestamp())
對於iOS使用此
為了將當前時間保存到Firebase數據庫中,我使用Unic Epoch Conversation:
let timestamp = NSDate().timeIntervalSince1970
對於將Unix紀元解碼到Date()的時間。
let myTimeInterval = TimeInterval(timestamp)
let time = NSDate(timeIntervalSince1970: TimeInterval(myTimeInterval))
您應該使用時間戳記(自1970年1月1日UTC的Unix Epoch以來的秒數) :
iOS(快速)
// Get current timestamp
let currentTimestamp = NSDate().timeIntervalSince1970
// Get date from timestamp
let date = NSDate(timeIntervalSince1970: TimeInterval(TimeInterval(yourTimestamp)))
android(Kotlin) (請注意,在android中,時間戳以毫秒為單位)
// Get current timestamp
val currentTimestamp = System.currentTimeMillis() / 1000 // We want timestamp in seconds
// Get date from timestamp
val date = Date(yourTimestamp * 1000) // Timestamp must be in ms to be converted to Date
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.