应用程序启动时的Firebase需要3秒钟才能加载数据 [英] Firebase on app startup taking more than 3 seconds to load data
问题描述
我需要完整的应用程序启动数据。为此,我在 onCreate 方法中写入了以下代码:
FirebaseDatabase database = FirebaseDatabase.getInstance ;
DatabaseReference myRef = database.getReference();
Log.i(FIREBASE,Firebase调用之前);
myRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
Log.i(FIREBASE,Call Completed);
//做我的工作,即构建我的UI
$ b @Override
public void onCancelled(DatabaseError databaseError){
Toast.makeText(context,请检查您的互联网连接,Toast.LENGTH_LONG);
}
});
现在我的问题是,Firebase需要4秒钟才能获取数据。在上面的代码中,两个日志语句之间有4秒的间隔,即Before Firebase Call& 呼叫完成
我甚至在真实设备上进行测试,即使在最少3秒的时间内也是如此。
我是否缺少这里的东西?
正如Doug Stevenson所说:首次加载数据,或者如果您身处的世界与Firebase服务托管的位置不同,那么我们建议您创建一个新的数据库。一个加载屏幕(启动画面)来加载显示您的主要活动之前的所有数据。
在此加载活动中,您可以使用 keepSynced
方法加载所有数据:
FirebaseDatabase.getInstance()getReference()keepSynced(真)。。
这种方法会将整个数据库下载到磁盘缓存。当你显示你的主要活动时,FIrebase会从这个缓存中加载数据。
通常,加载屏幕在屏幕上停留1000-1500毫秒。但是由于您所说的数据需要3秒钟,所以我建议创建一个播放3000毫秒动画的加载屏幕(或一组结合最后3000毫秒的动画)。如果您不知道如何制作动画,可以观看 Chet Haase的DevBytes视频 。
I have my read-only data in firebase, It's about 50KB. And I need the complete data on app startup. For that I wrote the following code in my onCreate method
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
Log.i("FIREBASE", "Before Firebase Call");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i("FIREBASE", "Call Completed");
// do my work i.e build my UI
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(context, "Please Check your Internet connection", Toast.LENGTH_LONG);
}
});
Now my problem is, Firebase is taking more than 4 seconds getting the data. In the above code there is 4 seconds gap between both log statements i.e 'Before Firebase Call' & 'Call Completed'
I even tested in real devices, even there its taking minimum 3 seconds. Am I missing something here?
As Doug Stevenson said: there should be a little delay when you're loading data for the first time, or if you're in a part of the world that is different than where the Firebase services happen to be hosted.
I suggest you create a loading screen (splash screen) to load all the data before showing your Main Activity.
In this Loading Activity, you can load all the data using the keepSynced
method:
FirebaseDatabase.getInstance().getReference().keepSynced(true);
This method will download your whole database to the disk cache. And when you show your Main Activity, FIrebase will load the data from this cache.
Generally, loading screens stay on screen for 1000-1500 milliseconds. But since you're saying the data takes 3 seconds, I recommend creating a loading screen that plays a 3000 milliseconds animation (or a set of animations that combined last 3000 milliseconds). If you don't know how to create animations, you can watch Chet Haase's DevBytes video.
这篇关于应用程序启动时的Firebase需要3秒钟才能加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!