Firebase setPersistenceEnabled(true)下载的数据如何 [英] Firebase setPersistenceEnabled(true) what about the downloaded data

查看:36
本文介绍了Firebase setPersistenceEnabled(true)下载的数据如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我获取数据的功能:

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();

    } 

假设我有10篇文章,由于以下原因,它们被保留在磁盘内存中: FirebaseDatabase.getInstance().setPersistenceEnabled(true);

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

现在,当我离线时,有人又添加了2篇文章,使第12篇文章成为可能.如果我上线并执行检索数据"功能,它会简单地调用内存中有10个子代的onchildadded和从Firebase数据库中调用2个新子代吗?还是会从firebase中下载所有12个子代?>

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 ?

推荐答案

当您为设备上已经存在的数据附加侦听器时,Firebase会立即用其在设备上拥有的数据满足侦听器.

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.

因此Firebase客户端将立即使用我们之前持久存在的10个子节点调用您的 onChildAdded .

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

然后它将请求发送到服务器以获取最新版本.它使用所谓的增量同步来完成此操作,这意味着它通过发送本地状态的哈希值(服务器将其与数据库中的当前状态进行比较)来实现.然后,服务器将增量发送回去,然后Firebase客户端将其用于更新其内部快照和磁盘上的状态.

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.

如果有任何更改,则Firebase客户端将触发正确的本地事件,以允许您的应用程序更新到新状态.因此,在添加了两个子节点的情况下,它将为每个子节点调用 onChildAdded .

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.

如果您要使用具有限制的侦听器,例如说 limitToLast(10),则Firebase客户端还将为不再使用的两个子代调用 onChildRemoved 在该查询中(因为它们是被新的孩子推出的).

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

这篇关于Firebase setPersistenceEnabled(true)下载的数据如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆