使用 Unity3d 在 Firebase 中存储 DateTime 值的最佳实践

Bo *_*sen 4 c# datetime unity-game-engine firebase firebase-realtime-database

我正在构建一个 Unity3d 2D 游戏,其中我使用 Firebase 作为数据库。我需要在数据库中存储相当多的DateTime值(如 UTC),我想知道这样做的最佳实践是什么。

这些DateTime字段将主要用于排序/过滤。

根据 Firebase 文档,我应该使用Firebase.Database.ServerValues.TIMESTAMP它只是一个object

问题是,我为我的 Unity 游戏使用了强类型模型,如下所示:

[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
}   
Run Code Online (Sandbox Code Playgroud)

当我保存/更新Profile我使用 UnitysJsonUtility.ToJson()方法时,如下所示:

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
});
Run Code Online (Sandbox Code Playgroud)

我不确定我应该utcCreated在我的模型上将字段设置为什么。试图简单地将该字段设为 an object,但未将任何值插入 Firebase。还试图使它有一个字符串.ToString()Firebase.Database.ServerValues.TIMESTAMP对象,但没有值插入火力地堡。

有谁知道如何做到这一点?;-) 或者只是关于在 Firebase 中存储日期和时间戳的最佳实践的任何帮助/提示?

提前致谢。

Bo *_*sen 5

好吧,对于任何其他不想花费数小时令人沮丧的问题的人来说,答案是:您不能(也许不应该,猜想这是有原因的)ServerValue.Timestamp在您的模型/对象上设置对象。

ServerValue.Timestamp是您通过调用SetValueAsync()UpdateChildrenAsync()

所以我所做的是在我的Profile类中添加两个字段:一个表示 unix 时间戳服务器值,另一个将 unix 时间戳服务器值表示为 C#DateTime对象:

using System;
using System.Collections.Generic;
using System.Linq;

[Serializable]
public class Profile
{

    [NonSerialized]
    public string profileKey;

    [NonSerialized]
    public long utcCreatedTimestamp;

    [NonSerialized]
    public DateTime utcCreated;

    // Serializable attributes
    public string userId;
    public string name;
    public int age;
    public string gender; // "M" or "F"

    public Profile() { }

    public Profile(Dictionary<string, object> fromFirebaseResult)
    {
        userId = fromFirebaseResult.ContainsKey("userId") ? fromFirebaseResult.First(x => x.Key == "userId").Value.ToString() : string.Empty;
        name = fromFirebaseResult.ContainsKey("name") ? fromFirebaseResult.First(x => x.Key == "name").Value.ToString() : string.Empty;
        age = fromFirebaseResult.ContainsKey("age") ? int.Parse(fromFirebaseResult.First(x => x.Key == "age").Value.ToString()) : 0;
        gender = fromFirebaseResult.ContainsKey("gender") ? fromFirebaseResult.First(x => x.Key == "gender").Value.ToString() : string.Empty;

        if (fromFirebaseResult.ContainsKey("utcCreatedUnix")) {
            long milliseconds;
            if(long.TryParse(fromFirebaseResult.First(x => x.Key == "utcCreatedUnix").Value.ToString(), out milliseconds)) {
                utcCreatedTimestamp = milliseconds;

                DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

                utcCreated = epoch.AddMilliseconds(milliseconds);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这个Profile类有一个默认构造函数和一个构造函数,它需要一个,Dictionary<string, object>因为这是我们从 Unity 查询它时从 Firebase 得到的。

Profile将新Profile对象保存到 Firebase 时,我创建了一个新对象,但没有设置与日期和时间相关的任何内容。将其保存在 Firebase 中,然后调用它在 FirebaseUpdateChildrenAsync中设置utcCreatedUnix字段。像这样:

DatabaseReference db = FirebaseDatabase.DefaultInstance.RootReference;

// Get the user id
string userId = GameManager.Instance.User.UserId;

// Get a reference to the profiles
var profiles = db.Child("profiles");

// Create a new serializable profile
Profile profile = new Profile {
    userId = userId,
    name = "Bo Mortensen",
    age = 34,
    gender = "M"
};              

// Serialize profile to JSON
string json = JsonUtility.ToJson (profile);     

// Push a new document to the database
var profileKey = profiles.Push ().Key;

// Set JSON for this document
profiles.Child (profileKey).SetRawJsonValueAsync (json, 1).ContinueWith (t => {
    if (t.IsCompleted) {
        // Assign the profile key (unique key generated by Firebase) to the profile
        profile.profileKey = profileKey;

        // Set the Firebase server timestamp on the datetime object
        profiles.Child(profileKey).UpdateChildrenAsync(new Dictionary<string, object> { { "utcCreatedUnix", ServerValue.Timestamp } });
    }
});
Run Code Online (Sandbox Code Playgroud)

Profile从 Firebase获取文档时,我现在可以执行以下操作:

var db = FirebaseDatabase.DefaultInstance.RootReference;

var profileRef = db.Child("profiles/MyProfileKey");

profileRef.GetValueAsync().ContinueWith(t =>
{
    var values = t.Result.Value as System.Collections.Generic.Dictionary<string, object>;

    // Let the constructor populate the fields
    Profile profile = new Profile(values)
    {
        profileKey = profileRef.Key
    };


    DateTime createdDate = profile.utcCreated;
});
Run Code Online (Sandbox Code Playgroud)

你可能会问为什么我不只是存储一个DateTime.UtcNow.ToString()我可以做到的,但我不想依赖客户端的时钟设置。通过使用 Firebases ServerValue.Timestamp,Firebase 确定正确的时间,而不是使用应用程序的单个客户端。

希望这对其他人有帮助:-)