Unity 中的 Firebase - 禁用持久性 [英] Firebase in Unity - Disable persistence
问题描述
我正在使用 Firebase 为 Android 制作一款手机游戏.该游戏有一个系统,允许玩家离线玩游戏并根据保存的日期时间更新/检索数据库中的数据.
I'm making a mobile game for Android using Firebase. This game has a system that allows the player to play offline and update/retrieve data from the database depending on the saved date-time.
这个日期时间(写在本地文件中)应该与保存在数据库中的日期时间相同,为此我写了这个:
This date-time (written in a local file) should be the same as the one saved in the database, and to do so I wrote this:
reference.Child("users/"+this.id+"/updateDate").SetValueAsync(updateDate).ContinueWith(task =>
{
if (task.IsCompleted) {
Debug.Log("Setting new update date into file: "+updateDate);
this.lastUpdateDate = updateDate;
WriteFile();
}
});
问题是,即使没有互联网连接,该值也会被修改,但永远不会满足以下条件,所以会发生以下情况:如果您读取该值,即使没有互联网连接,您也会读取新"日期时间,而文件仍包含旧"日期时间(实际在数据库中的日期时间).
The problem is that, even if there is no internet connection, the value gets modified but the condition below is never satisfied so what happens is this: If you read the value, even without internet connection, you read the "new" date-time, while the file still contains the "old" one (the one actually in the database).
我知道最好的解决方案是删除所有内容并让离线持久性发挥作用,但我做了一些测试,它甚至不适用于 Android 的构建.所以我的问题如下:
I know the best solution is to delete everything and let offline persistence do its work, but I did a couple tests and it doesn't even work on Android's build. So my question is the following:
有没有办法在 Unity 中禁用离线持久性?
因为我已经尝试了 "FirebaseDatabase.DefaultInstance.setPersistenceEnabled(false)" 但似乎该函数甚至不存在于 Unity 的 API 中(它给了我编译错误).
Because I've tried "FirebaseDatabase.DefaultInstance.setPersistenceEnabled(false)" but it seems like that function is not even present in the Unity's API (it gives me compile errors).
感谢您的帮助!
推荐答案
要禁用持久性,请执行以下操作:
To disable persistence, do:
Firebase.FirebaseDatabase.DefaultInstance.SetPersistenceEnabled(false);
如果您来自 Google,并且无法初始化持久性存储"使用 2 个编辑器测试 Unity 时崩溃,请执行
If you're coming from Google with the "Failed to initialize persistence storage" crash when testing Unity with 2 editors, do
#if UNITY_EDITOR
Firebase.FirebaseDatabase.DefaultInstance.SetPersistenceEnabled(false);
#endif
这篇关于Unity 中的 Firebase - 禁用持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!