1

here is my function to get data :

public void retrievedata(){
    FirstRef.child(obj.getsEventID()).orderByChild("date").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s)
        {

            if (dataSnapshot.exists())
            {
                DisplayMessages(dataSnapshot);
            }

        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s)
        {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}


private void DisplayMessages(DataSnapshot dataSnapshot) {
    Iterator iterator = dataSnapshot.getChildren().iterator();




        String Article = (String) ((DataSnapshot) iterator.next()).getValue();
        String Key = (String) ((DataSnapshot) iterator.next()).getValue();
        String Organisateur = (String) dataSnapshot.child("name").getValue().toString();
        String date = (String) dataSnapshot.child("date").getValue().toString();


         Date resultdate = new Date(Long.parseLong(date));
         String date2 = DateFormat.format(resultdate).toString();


        ListOfArticles.add(0,new ListItemTypeOne(Key, Article, Organisateur, date2));




        adapter.notifyDataSetChanged();

    } 

Let's suppose I have 10 articles, they are kept in the disk memory thanks to : FirebaseDatabase.getInstance().setPersistenceEnabled(true);

Now, while I was offline someone added 2 more articles which makes 12. If I go online, and execute the function "retrieve data", will it simply call the onchildadded with the 10 child in the memory and the 2 new child from the firebase database or will it download all of the 12 childs from firebase ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
ImNeos
  • 527
  • 6
  • 17

2 Answers2

0

The previously cached 10 children will be loaded from disk and not transferred from the server again.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • can you please check : https://stackoverflow.com/questions/55578264/firebase-download-data-again-from-serveur It seems that it is well downloaded from the server again – ImNeos Apr 08 '19 at 19:59
0

When you attach a listener for data that is already present on the device, Firebase immediately satisfies the listener with the data that it has on the device.

So the Firebase client will immediately call your onChildAdded with the 10 child nodes that we persisted earlier.

It then sends a request to the server to get the most up to date version. It does this with a so-called delta-sync, meaning that it by sending a hash value of the local state, which the server compares to the current state in the database. The server then sends the delta back, which the Firebase client then uses to update its internal snapshot and the state on disk.

If there are any changes, the Firebase client then fires the correct local events to allow your application to update to the new state. So in the case where two child nodes were added, it will call onChildAdded for each of those.

If you were to use a listener with a limit, say limitToLast(10), then the Firebase client would also call onChildRemoved for the two children that are no longer within that query (since they were pushed out by the new children).

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Ok I understand But I have made the test with your example "limitToLast(10)" Result: - 1)If I have 10 articles and add 2 mores, I have 12 articles in my list. Ok normal because I didn't put a method OnChildDeleted -2)If I go out of the activity and reload it, it will load only the 10first articles --->now here is the thing I want to know about the 2) case : =>something must have uptaded the data to "push" the 2 children and can't be my function as I didn't put any onChilddeleted method. Did it only download the 2news articles and my app automatically push the others? – ImNeos Mar 17 '19 at 18:27
  • 1
    1) That's because you're ignoring `onChildRemoved`. 2) As I described, the Firebase client updates its internal copy of the data. So if you attach a new listener, it will get the up to date snapshot. – Frank van Puffelen Mar 17 '19 at 20:23
  • Ok thank you sir. But you said it loads first the file from the disk memory and only then it checks if there is any uptade and fire the fonctions (on child added in this case). So can you tell me why if I don't put limitTofirst() (the original fonction I showed) and if I delete 5 items without having onchildremoved method () they will still be removed from my list ? They shouldn't because first it loads data from disk and only then update – ImNeos Mar 17 '19 at 22:48
  • 1
    If it still fires with the child nodes that have been removed from the server, those child nodes must be coming from the local cache. If it doesn't fire for those deleted child nodes, they must have been deleted from the local cache. – Frank van Puffelen Mar 18 '19 at 00:09