当我在UI上使用firebase初始化数据和setText
时,它被延迟了10~15秒。
另一方面,当我使用Volley时,它只延迟了1秒钟。
为什么我使用firebase时速度太慢?如何修改此代码?
这是我的代码(使用ValueEventListener
)和JSON树
"couple_profile" : {
"TEST999AhCBcHK32AQzU3JglVbAxhTD6Mn1" : {
"birthday_1" : "1994-06-29",
"birthday_2" : "19940629",
"couple_nick" : "TEST999",
"exp" : 0,
"gold" : 0,
"level" : 1,
"member" : {
"AhCBcHK32AQzU3JglVbAxhTD6Mn1" : {
"FCM" : "d5Y-fl_0VCs:APA91bHX7HA531PXc1e4clUg61uB3XsKlwmQ_2U9OaESUTP0r-dnwbitSvxQ4EDMqPzK-t_5b9qPOhLm01FRffgB9-Ot6bZmx1JzwZvc07yoyhashGUS79E7Dztr2J7NfR1NGXw1LT2V",
"birthday" : "1994-06-29",
"name" : "김준겸",
"phone_number" : "01050639201"
},
"FMsH7leNTOXBgbOFoWdaTMjt05T2" : {
"birthday" : "19940629",
"name" : "kimGod",
"phone_number" : "1541"
}
},
"mileage" : 0,
"name_1" : "김준겸",
"name_2" : "kimGod",
"uid_1" : "AhCBcHK32AQzU3JglVbAxhTD6Mn1",
"uid_2" : "FMsH7leNTOXBgbOFoWdaTMjt05T2"
}
private void get_home_info() {
valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
coupleProfile = dataSnapshot.getValue(CoupleProfile.class);
if (coupleProfile == null) {
Log.e(TAG, "User " + FirebaseAuth.getInstance().getCurrentUser().getEmail() + " is unexpectedly null_1");
Toast.makeText(Home_Fragment.this.getContext(), "Error_1", Toast.LENGTH_SHORT).show();
} else {
StorageReference storageRef2 = storage.getReferenceFromUrl("FirebaseURL").child("Profile_image/" + FirebaseAuth.getInstance().getCurrentUser().getUid());
Glide.with(getContext()).using(new FirebaseImageLoader()).load(storageRef2).centerCrop()
.bitmapTransform(new CropCircleTransformation(new CustomBitmapPool()))
.into(me_view);
SharedPreferences.Editor sh = home_info.edit();
if (My_UID.equals(coupleProfile.uid_1)) {
my_number = 1;
sh.putString("my_name",coupleProfile.name_1);
sh.putString("op_name",coupleProfile.name_2);
sh.apply();
my_name_text.setText(coupleProfile.name_1);
op_name_text.setText(coupleProfile.name_2);
state_text.setText(coupleProfile.state_1);
op_state_text.setText(coupleProfile.state_2);
StorageReference storageRef = storage.getReferenceFromUrl("FirebaseURL").child("Profile_image/" + coupleProfile.uid_2);
Glide.with(getContext()).using(new FirebaseImageLoader()).load(storageRef).centerCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.bitmapTransform(new CropCircleTransformation(new CustomBitmapPool()))
.into(friend_view);
} else if (My_UID.equals(coupleProfile.uid_2)) {
my_number = 2;
sh.putString("my_name",coupleProfile.name_2);
sh.putString("op_name",coupleProfile.name_1);
sh.apply();
my_name_text.setText(coupleProfile.name_2);
op_name_text.setText(coupleProfile.name_1);
state_text.setText(coupleProfile.state_2);
op_state_text.setText(coupleProfile.state_1);
StorageReference storageRef = storage.getReferenceFromUrl("FirebaseURL").child("Profile_image/" + coupleProfile.uid_1);
Glide.with(getContext()).using(new FirebaseImageLoader()).load(storageRef).centerCrop().diskCacheStrategy(DiskCacheStrategy.SOURCE)
.bitmapTransform(new CropCircleTransformation(new CustomBitmapPool()))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(friend_view);
} else {
Log.e(TAG, "User " + FirebaseAuth.getInstance().getCurrentUser().getEmail() + " is unexpectedly null_2");
Toast.makeText(Home_Fragment.this.getContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled: ", databaseError.toException());
}
};
ref = databaseReference.child("couple_profile").child(room_token);
ref.addValueEventListener(valueEventListener);
}
答案 0 :(得分:1)
我认为如果你从firebase使用这个方法,你将有更好的数据加载:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
使用上面的代码,解释一下:
Firebase在离线数据时提供了极大的支持。它在没有互联网连接时自动离线存储数据。当设备连接到互联网时,所有数据都将被推送到实时数据库。但是,即使应用程序重新启动,启用磁盘持久性也会使数据脱机。
同时尝试并检索onStart()