是在查询而不是DatabaseRef上使用keepSynced()有什么不同吗? [英] Is using keepSynced() on Query instead of on DatabaseRef make any difference?

查看:84
本文介绍了是在查询而不是DatabaseRef上使用keepSynced()有什么不同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正从Firebase获取单个dataSnapshot,如下所示:

I am currently getting a single dataSnapshot from Firebase like so:

public Task<DataSnapshot> getLatestMessage(@NonNull String roomId) {
    final TaskCompletionSource<DataSnapshot> source = new TaskCompletionSource<>();
    DatabaseReference dbRef = mDatabase.getReference(NODE_MESSAGES).child(roomId);
    dbRef.keepSynced(true);
    ValueEventListener listener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            source.setResult(dataSnapshot);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            source.setException(databaseError.toException());
        }
    };
    Query query = dbRef.orderByKey().limitToLast(1);
    query.addListenerForSingleValueEvent(listener);
    return source.getTask();
}

请注意,我已经在dbRef对象上调用了keepSynced().

Notice that I called keepSynced() on dbRef object already.

这是示例数据结构:

/root
  /messages
    /$roomId
      /$messageId
        /content
        /timestamp
        /etc...

我能够按预期获取最新的单个快照数据,但是我想知道,如果将keepSynced()调用移到Query对象而不是DatabaseReference中,这有什么区别吗?即

I am able to get the most recent single snapshot data as expected, but I was wondering, does it make any difference if I move the keepSynced() call in the Query object instead of the DatabaseReference? i.e.

    // dbRef.keepSynced(true); >> REMOVE THIS <<
    ValueEventListener listener = new ValueEventListener() {...};
    Query query = dbRef.orderByKey().limitToLast(1);
    query.keepSynced(true); // >> ADD THIS <<
    query.addListenerForSingleValueEvent(listener);

我们目前在Firebase上平均每天(每天)有50%的负载,并且随着用户的不断涌入,我想知道它是否可以以某种方式改善应用程序中的任何功能,尤其是负载方面.我什至尝试过这样愚蠢的事情:

We're currently averaging 50% load (per day) on Firebase right now and with the steady inflow of users, I was wondering if it could improve anything in the app somehow, specially with the load. I even tried something as silly as this:

    dbRef.keepSynced(true);
    ValueEventListener listener = new ValueEventListener() {...};
    Query query = dbRef.orderByKey().limitToLast(1);
    query.addListenerForSingleValueEvent(listener);
    dbRef.keepSynced(false);

-首先启用keepSynced(),以确保引用指向最新版本,然后在查询并添加侦听器后将其禁用.不幸的是,它没有提供新数据,就像保持启用状态一样.

-- enabling keepSynced() at the start to make sure that the reference is pointing to the most recent version, then disabling it after querying and adding the listener. Unfortunately, this doesn't provide the fresh data, not like when keeping it enabled.

我已经阅读了优化数据库性能文档,并相信我按照需要遵循了建议的做法.

I've already gone through the Optimization DB Performance documentation and believe that I followed the suggested practices as needed.

推荐答案

limitToLast()查询上放置keepSynced()调用不会对数据库服务器上的负载产生任何影响.服务器需要加载完全相同的数据并对其进行监视,它只将最后一项返回给客户端.

Putting the keepSynced() call on a limitToLast() query will not make any difference on the load on the database server. The server needs to load the exact same data and monitor it, it just only returns the last item to the client.

我建议在您的应用中谨慎使用keepSynced.每次对keepSynced的调用都会为您附加到的引用/查询保留一个空的侦听器.这意味着,即使用户不在看那个聊天室,每个客户端也都有一个活跃的监听器来监听您在其上呼叫keepSynced的每个聊天室.虽然这可能恰好是应用程序用例的正确选择,但这会限制应用程序的可伸缩性.

I recommend using keepSynced sparingly in your app. Each call to keepSynced keeps an empty listener to the reference/query you attach it to. That means that each client has an active listener to each chat room you call keepSynced on, even when the user is not looking at that room. While that may be precisely the right call for the use-cases of your app, it will limit the scalability of your app.

如果您担心达到峰值负载,则可能需要考虑研究如何

If you're worried about reaching peak load, you might want to consider looking into how to shard your data over multiple databases. Chat apps are typically relatively easy to shard, since each chat room is already isolated.

这篇关于是在查询而不是DatabaseRef上使用keepSynced()有什么不同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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