Firebase setPersistenceEnabled(true) what about the downloaded data

雨燕双飞 提交于 2021-01-28 08:47:33

问题


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 ?


回答1:


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).




回答2:


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



来源:https://stackoverflow.com/questions/55209813/firebase-setpersistenceenabledtrue-what-about-the-downloaded-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!