Reputation: 533
My app using firebase realtime database and I setPersistenceEnabled(true) to make it still work when device goes offline. Everything is ok when device offline, every operation like add, change, and remove data can work fine.
Problem come when device come back online after do too many operation for example delete much data when offline. After I see many data in my realtime database was delete simultaneously, I cannot do operation like add and change data for several times (5-10 minute) and it depend on how much data I was delete when offline. I can only see my add and change operation after that time.
My question is how many operation firebase can handle when offline and make it still smooth when it come back online, so my app not like freeze for several time when user add and change another data?, or
is there any trick to make it smooth when transition from offline to online when I have many operation to do in offline mode?
below is my code to update my data before I delete it, and when offline my app can run this block of code until 100 times for example in 1 hour:
public void ritase(String number){
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("/vehicles/").child(number);
HashMap<String, Object> childUpdates = new HashMap<String, Object>();
childUpdates.put("ritase/from", from);
childUpdates.put("ritase/to", to);
childUpdates.put("ritase/accept", accept);
childUpdates.put("ritase/userid", userid);
childUpdates.put("ritase/passenger", passenger);
childUpdates.put("ritase/stat", 1);
childUpdates.put("ritase/time_out", time);
childUpdates.put("trips/content/time_out", time);
childUpdates.put("trips/content/stat", 1);
ref.updateChildren(childUpdates);
ref.removeValue();
}
and after my app back to online again, below block of code operation was stuck for several minutes. Stuck here means I cannot see the change immediately in my app:
Public void updateStatus(String number){
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("/vehicles/").child(number);
HashMap<String, Object> childUpdates = new HashMap<String, Object>();
childUpdates.put("driver/name", name);
childUpdates.put("driver/nip", nip);
if (PrefHelper.getPref(context,VarConstant.ISBARCODE) == "true") {
childUpdates.put("is_barcode", 1);
} else {
childUpdates.put("is_barcode", 0);
}
childUpdates.put("position/content/location", location);
childUpdates.put("position/content/time_in", time);
childUpdates.put("position/content/time_out", 0);
childUpdates.put("position/content/stat", 0);
childUpdates.put("position/content/username", username);
int status;
if (PrefHelper.getPref(context, VarConstant.TYPE).contains(VarConstant.PERIMETER)) {
status = 0;
childUpdates.put("trips/content/status", status);
childUpdates.put("trips/content/time_start", 0);
childUpdates.put("trips/content/time_stop", 0);
} else {
status = 2;
childUpdates.put("trips/content/status", status);
childUpdates.put("trips/content/time_start", time);
childUpdates.put("trips/content/time_stop", time);
}
ref.updateChildren(childUpdates);
}
I also try to use completion listener for removeValue(), but it fire very late even in firebase realtime database the data was delete.
Upvotes: 2
Views: 561
Reputation: 599166
When you use disk persistence, Firebase stores two types of data on disk:
It also keeps an in-memory copy of all active data, which means: any data that you're currently listening for. In this in-memory copy, the pending writes are applied to the data.
When you restart your app and attach a listener, Firebase loads the cached data and applies any pending writes it has. This process has linear performance, and can be quite slow due to the hashing algorithm used.
This means that restarting an app that has a lot of cached data with many pending writes can indeed take some time. This is expected behavior.
To reduce the startup time, limit the amount of data you keep offline, and limit the number of pending writes.
Upvotes: 0