I use firebase real time database.
I have set : "FirebaseDatabase.getInstance().setPersistenceEnabled(true);"
So ok perfect, when I'am offline, all the data are being loaded just like I was online.
But I have a few questions.
In my app, I have chat group with few people. Let's assume someone is in a group and is offline. He writes what he wants in the chat and the messages will be send when he will turn online. But, while he is offline, someone kicks him out of the group. How do I tell firebase :
if (the user still belongs to the group) { sync messages } else { don't sync messages }I have tried to check if the users still belong to the group while he is offline but when it goes back online everything has already been checked in offline mode so the data are just sent without reckecking again.
Let's imagine I have 50 messages in my chat. While I'am offline, I get 5 messages. When I go back online, will it redownload the 55 messages or just the 5 extras message?
I am just trying to figure out if I the firebase sync is going to cost me a lot of data or not.
EDIT
Here is the way I retrieve data from chat :
public void retrievedata() {
UserMessageRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) /**** HERE IS THE PROBLEM***/
{
if (dataSnapshot.exists()) {
DisplayMessages(dataSnapshot);
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
//Nothings
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
//Nothings
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { //Nothings
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
//Nothings
}
});
} //function to retrieve messages
private void DisplayMessages(DataSnapshot dataSnapshot) { Iterator iterator = dataSnapshot.getChildren().iterator();
String chatImage = (String) ((DataSnapshot) iterator.next()).getValue();
String chatMessage = (String) ((DataSnapshot) iterator.next()).getValue();
String nID = (String) ((DataSnapshot) iterator.next()).getValue();
String chatTime = (String) ((DataSnapshot) iterator.next()).getValue().toString();
ListOfChats.add(new ChatClass(nID, chatMessage, chatTime, chatImage));
adapter.notifyDataSetChanged();
} Now here is the problem.
If I'am offline and go into the chat, the messages are just retrieved from disk, ok works fine.
BUT If I go online and I got 2 new messages : instead of calling the onChildAdded for the 2 news messages, it download ALL of the messages and THEN call the onChildAdded with the old child and the 2 news child. What I want, is that the 2 news messages are added to the local json file in my phone and that everyting isnt downloaded everytime I open the conservation.
So with a firebase database which is about 30KB, I got 5-10 MB of downloads everyday even If I add 2KB in it per day... (It's for university project so we are like 2 users for now)
So is there a solution to this (firebase just update the json file into the local database without downloading eveything) or do I have to do my own SQL database and make a smart sync adapter?
I am sure that firebase must have done something smart about this.