Java – valueeventlistener childeventlistener for recyclerview in Android

Firebase database users know that there are two basic listeners for listening to data: valueeventlistener and childeventlistener When we listen to an object, it works very well. When we listen to some objects, it becomes quite difficult

To point out the problem, let's assume that we have Hacker News Feed. Let's listen to the "posts" object in firebase

Of course, there are recyclerview posts in our application. I think the best idea is to use firebaseui, but the problem is that we want to use a more abstract application when changing the server or testing So we'll use some adapters, but this is another question

I mentioned two listeners. The question is which is better?

When we use valueeventlistener, we will get the whole collection, but if there is any change, like a user changing the content, we will have to reload the whole data, which means that more bytes are sent through expensive network transmission Another problem is that when we use multiple users, here is an example:

>Post has a userid, but we want to display its name, so in ondatachanged method, we get the user data, as shown below:

postsReference.addValueEventListener(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {
          for (DataSnapshot data : dataSnapshot.getChildren()) {
               Post post = data.getValue(Post.class);   
               usersReference.child(post.getUserId()).addListenerForSingleValueEvent(new ValueEventListener() {

                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            // Here we have user data
                        }

                        @Override
                        public void onCancelled(FirebaseError firebaseError) {

                        }
              });
          }
     }

     @Override
     public void onCancelled(FirebaseError firebaseError) {
     }
});

As you can see, now we have to add each post to recyclerview separately, which will prompt us that maybe we should use childeventlistener

So now when we use childeventlistener, the problem is the same - we have to add each post to recyclerview separately, but when someone changes the post content, firebase will only send us this post, which means reducing data through the network

We don't like to add post recyclerview alone because, for example: – it's difficult to add a load indicator because we don't know when all data appears. – Users constantly refresh the view, and new posts become visible instead of the entire list It is difficult to sort the collection, and we must do so in the adapter

topic

What are the best practices collected using firebase and may be better solutions than those written above?

edit

The data scheme will be as follows:

"posts" : {
    "123456" : {
      "createdAt" : 1478696885622,"content" : "This is post content","title" : "This is post title","userId" : "abc"
    },"789012" : {
      "createdAt" : 1478696885622,"content" : "This is post content 2","title" : "This is post title 2","userId" : "efg"
    }
  }
  "users" : {
    "abc" : {
      "name" : "username1"
    },"efg" : {
      "name" : "username2"
    }
  }

Edit 2

I made a mistake – > when things change, firebase won't get the whole data in valueeventlistener It only gets "delta", here is proof

Solution

This problem has several problems (i.e. performance, progress indicators, processing new data, such as sorting) Of course, you should propose a solution that takes into account the priority of your requirements Both valueeventlistener and childeventlistener of IMO have their use cases:

>Childeventlistener is usually the recommended way to synchronize object lists This is even mentioned in the document on working with lists:

This is because your client only receives changed children with specific updates (add or remove), not the entire list at each update Therefore, it allows for finer handling of list updates. > Valueeventlistener can be more useful when you need to handle the entire list to modify a child This is the case when you have to sort the list in recyclerview This is easier by taking the entire list, sorting it, and refreshing the view's dataset On the other hand, using childeventlistener is more difficult to sort, because only a specific child node can be accessed in the list of each update event

From the perspective of performance, since even valueeventlistener is aware of "increment" when synchronizing updates, I think it is less efficient on the client side, because the client must update the whole list, but it is far better than the low efficiency on the network

With regard to continuous refresh and progress indicators, the most important thing to note is that firebase database is a real-time database, so the continuous provision of real-time data is an inherent feature If you do not need to update the event, you only need to use the addlistenerforsinglevalueevent method to read the data only once You can also use this option if you want to display a progress indicator when the first snapshot is loaded:

// show progress indicator
postsReference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // load initial data set
        // hide progress indicator when done loading
    }

    ...
});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>