Firebase的addValueEventListener响应时间有点慢吗? [英] Firebase addValueEventListener response time little slow?

查看:58
本文介绍了Firebase的addValueEventListener响应时间有点慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Firebase上使用Android SDK中的addValueEventListener检索数据,但是我发现有时响应时间至少需要1分钟才能得到结果.

I am working on Firebase to retreive data using addValueEventListener from Android SDK but i found sometime the response time take minimum 1 minute to get the result.

我的代码:

Firebase firebase = new Firebase("https://example.firebaseio.com/");
firebase.child("XYZ").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    Log.d("DataFirebase","onDataChange : "+dataSnapshot);

                    Toast.makeText(getApplicationContext(),"onDataChange",Toast.LENGTH_SHORT).show();

                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                    Log.d("DataFirebase","onCancelled : "+firebaseError);

                    Toast.makeText(getApplicationContext(),"onCancelled",Toast.LENGTH_SHORT).show();
                }
    });

这是我上面的代码,请让我知道,我怎么能立即从键中得到结果.请给我建议解决方案.

This is my above code , please let me know , how can i get the result instantly from a key. Please suggest me some solution.

推荐答案

我也有Firebase实时数据库运行缓慢的问题.尝试在Application类的"onCreate"方法中调用FirebaseDatabase.getInstance().setPersistenceEnabled(true);.

I too have a problem of slowness with firebase realtime db. Try calling, FirebaseDatabase.getInstance().setPersistenceEnabled(true); inside your Application class's "onCreate" method.

请注意,这将在您的设备上保存数据,并且第二次及以上的调用会更快,但会从缓存中删除,并且可能会过时,请阅读

Be aware that this will save data on your device and the second call and above will be much faster but from cache and can be outdated, Read this for handling syncing data from server when needed immediately.

这篇关于Firebase的addValueEventListener响应时间有点慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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