Firebase 侦听器的首次调用需要时间 [英] Firebase listeners first time invocation takes time

查看:29
本文介绍了Firebase 侦听器的首次调用需要时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次监听器触发需要很多时间(大约 40 秒),后续加载需要更少的时间(大约 1 秒),如何加快第一次加载?

First time listener firing takes so much time (approx. 40s), subsequent loads takes lesser time (approx. 1s), how can speed up the first time load as well?

我已经给了,

    FirebaseDatabase.getInstance().setPersistenceEnabled(true);

活动开始时.

        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("");
        ref.keepSynced(true);
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

推荐答案

如果您正在使用侦听器,则需要知道您正在尝试通过 Internet 读取数据.您无法将其与尝试读取存储在本地磁盘上的 SQLite 数据库进行比较.从 Firebase 服务器获取数据的速度取决于您的互联网连接速度以及您尝试获取的数据量.所以最有可能等待这么多的原因是其中之一.如果原因是数据量大,请尝试优化您的查询或尝试小部分获取数据.

If you are using a listener, you need to know that you are trying to read data over the internet. You cannot compare this with an atempt to read a SQLite database, which is stored locally on disk. The speed of getting the data from Firebase servers, depends on the speed of your internet connection and on the ammount of data that you are trying to get. So most likely the reason for waiting so much is one of this. If the reason is the ammount of data, try to optimize your queries or try to get the data in small parts.

如果我们谈论的是第一次尝试读取记录,它可能比后续尝试慢,因为它必须启动互联网连接.我知道 Firebase 团队正在努力提高性能,但在通过网络检索数据时您不能期望 0ms.

If we are speaking abot the first atempt to read a record, it might be slower than the subsequent ones, because it has to initiate the internet connection. I know that Firebase team is trying to improve the performance, but you can't expect 0ms when retrieving data over a network.

根据您的评论,我需要再告诉您一些事情.当您连接到服务器时,无法强制从缓存中检索数据,因为您无法在未连接到服务器时停止从缓存中检索数据.

According to your comment, I need to tell you a few more things. There is no way to force the retrieval of the data from the cache while you're connected to the server, as you cannot to stop the retrieval of the data from the cache while you are not connected to the server.

Firebase 旨在在设备永久离线或您的应用程序暂时失去网络连接且您无法更改此行为时从缓存中检索数据.

Firebase is desinged to retrieve data from the chache when the device is permanently offline or while your application temporarily loses its network connection and you cannot change this behaviour.

因此要获得 FirebaseDatabase 对象,您需要使用以下代码行一次:

So to get the FirebaseDatabase object you need to use the following line of code once:

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();

然后要获得DatabaseReference,您需要使用以下行.

Then to get a DatabaseReference, you need to use the following line.

DatabaseReference rootRef = firebaseDatabase.getReference();

我相信您在活动中需要不止一个参考.假设您将使用两个:

I'm sure you'll need to in your activity more then one reference. Let's say you'll use two:

DatabaseReference usersRef = rootRef.child("users");
DatabaseReference postRef = rootRef.child("post");

您现在可以为这些引用中的每一个添加一个侦听器,如下所示:

You can now add a listener for on each one of these references like this:

ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        //code to get the data
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);

删除监听器的代码如我在这个发布.

And the code the remove the listener is as explained in my answer from this post.

不要忘记,onDestroy() 并不总是被调用.

Don't forget, onDestroy() is not always called.

总而言之,您创建一个数据库连接,使用您需要的多个引用,相应地添加侦听器,然后根据您活动的生命周期删除.

As a conclusion, you create a single database connection, use as many references you need, add the listeners accordingly, remove then according to the life-cycle of you activity.

这篇关于Firebase 侦听器的首次调用需要时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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