我正在構建Unity3d 2D遊戲,並將Firebase作爲數據庫使用。我需要在數據庫中存儲相當多的DateTime
值(作爲UTC),我想知道做這件事的最佳做法是什麼。使用Unity3d在Firebase中存儲DateTime值的最佳做法
DateTime
字段將主要用於排序/過濾。
按照火力地堡的文件,我應該利用Firebase.Database.ServerValues.TIMESTAMP
這僅僅是一個object
的事情是,我使用的是強類型的模型,我團結的遊戲,像這樣的:
[Serializable]
public class Profile {
[NonSerialized]
public string ProfileKey;
// Serializable attributes
public string userId;
public string name;
public int age;
public string gender; // "M" or "F"
public DateTime utcCreated; // UTC
public DateTime utcUpdated; // UTC
}
和我使用Unitys JsonUtility.ToJson()
方法,這樣當我保存/更新Profile
:
var profiles = db.Child ("profiles");
Profile profile = new Profile {
userId = GameManager.Instance.User.UserId,
name = "My new profile",
age = 23,
gender = "M",
utcCreated = ?? // Should somehow use Firebase.Database.ServerValues.TIMESTAMP here, I guess?
};
string json = JsonUtility.ToJson (profile);
var profileKey = profiles.Push().Key;
profiles.Child (profileKey).SetRawJsonValueAsync (json, 1).ContinueWith (t => {
// .. handle response here
});
我不確定我應該在我的模型上設置utcCreated
字段。試圖簡單地將該字段設置爲object
,但沒有值插入到Firebase中。還嘗試將其設爲.ToString()
至Firebase.Database.ServerValues.TIMESTAMP
對象的字符串,但未在Firebase中插入任何值。
有沒有人有一個想法如何做到這一點? ;-)或者只是關於在Firebase中存儲日期和時間戳的最佳做法的任何幫助/提示?
在此先感謝。