Firebase侦听器第一次调用需要花费时间 [英] Firebase listeners first time invocation takes time

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

问题描述

第一次触发监听需要花费大量时间(大约40s),随后的加载花费更少的时间(大约1s),那么如何加快第一次加载的速度呢?

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服务器获取数据的速度取决于您的Internet连接速度以及您要获取的数据量.因此,等待如此之多的原因很可能是其中之一.如果原因是数据量很大,请尝试优化查询或尝试将数据分成小部分.

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.

如果我们说的是自动读取记录的第一个尝试,它可能会比后续记录慢,因为它必须启动Internet连接.我知道Firebase团队正在努力提高性能,但是通过网络检索数据时,您不能指望0毫秒.

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天全站免登陆