Firebase保持同步 [英] Firebase keepsynced

查看:57
本文介绍了Firebase保持同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Unity项目中使用Firebase时,为了获得简单的高分,我在执行查询时偶然发现了问题.在编辑器中,所有内容都像一个超级按钮(编辑器没有持久性)在启用了持久性的设备上,故障开始了.查询显示的是缓存的Firebase数据,因此只有在客户端第一次调用时它才是正确的,然后在Firebase认为它适合同步时才可能正确(可能永远不会,因为没有事件处理程序)

Working with Firebase in a Unity project, for a simple highscore, I stumbled on problems when doing a query. In editor, everything works like a charm (Editor does not have persistence) On devices (with persistence enabled) the troubles begin. Query is showing cached Firebase data, so it is only correct on first ever call in the client, and then when Firebase sees it fit to sync (maybe never, since there is no eventhandler)

但是,在寻找解决方案时,没有办法强制更新缓存的值.然后,我在查询中对KeepSynced(true)进行了测试,这似乎可行:

However, looking for a solution, there is no way to force an update of the cached values. I then tested with KeepSynced(true) on the query and this seems to work:

this.HiscoreQuery = hiscoreref.OrderByChild ("Score").LimitToLast (20);
this.HiscoreQuery.KeepSynced(true);
this.HiscoreQuery.GetValueAsync().ContinueWith (task => {
  if (task.IsFaulted) {
    Debug.LogError ("Get hiscores faulted");
    return;
  }
  if (task.Result != null && task.Result.ChildrenCount > 0) {
    Debug.Log ("Get hiscore data success!");
    this.AddDelayedUpdateAction (() => this.OnGetHiScores (task.Result));
  }
});

问题:虽然如果Firebase仅侦听查询的LImitToLast(20)可能很好,但是如果Firebase内部在每个客户端中复制了整个(不断增长的)hiscorelist,那将是一件非常糟糕的事情.

Question: While this can be fine if Firebase only listen for the Query's LImitToLast(20), it would be a very bad thing, if the Firebase internally is keeping the whole (growing) hiscorelist copied in every client.

有人知道 KeepSynced(true)是否限于实际查询范围或整个树/分支吗?以及如何验证这一点?

Does anyone know if KeepSynced(true) is limited to the actual query scope or the whole tree/branch? And how could one validate this?

推荐答案

GetValue 调用不适用于 KeepSynced(true).Firebase会急切地从缓存中返回值,然后才从服务器加载数据.

GetValue calls don't work well with KeepSynced(true). Firebase eagerly returns you the value from the cache, and only then loads the data from the server.

更多说明,请在这里查看我的答案: Firebase离线功能和addListenerForSingleValueEvent

For a longer explanation, see my answer here: Firebase Offline Capabilities and addListenerForSingleValueEvent

如果要使用缓存,请使用侦听器而不是 GetValue 调用.使用侦听器,您的回调将被触发两次(如果有更改):一次使用来自缓存的值,然后一次使用来自服务器的值.

If you want to use caching, use listeners and not GetValue calls. With a listener, your callback will be fired twice (if there is a change): once with the value from the cache, and then once with the value from the server.

这篇关于Firebase保持同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆