Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

java - ValueEventListener vs ChildEventListener for RecyclerView in Android

Firebase Database users know that there are two basic listeners for listening Data: ValueEventListener and ChildEventListener. It works great when we listen for one object, but becomes quite difficult when we listen for some collection.

To specify question, let's imagine that we have HackerNews feed, and we listen e.g. "posts" object in Firebase.

Of course we have RecyclerView in our app for displaying posts and I think good idea would be using FirebaseUI, but the problem is that we want to make more abstract application in case of changing server side or tests. So we would use some adapters, but this is another question.

We have two listeners as I mentioned, question is which is better?

When we use ValueEventListener we will get whole collection, but in case of any changes, like one user changed content of the post, we will have to reload whole data, which means more bytes sending via expensive network transfer. Another problem is when we use multilisteners, and here is example:

Post has userId, but we want to display his name, so in onDataChanged method we have fetch user data, like this:

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) {
    }
});

You can see that now we have to add each post to RecyclerView separately, which would give us clue 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 post content, firebase sends to us only this one post, which means less data via network.

We don't like adding post separately to RecyclerView, because e.g.: - It's hard to add loading indicator, cause we don't know when all data comes. - Users gets constantly refreshing view, while new posts comes instead of whole lists becomes visible. - It's hard to sort that collection, we will have to do that maybe in adapter.

Question

What are best practices for using firebase with collection, and maybe better solutions than I wrote above?

EDIT

Data scheme would look like this:

"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 -> Firebase is not fetching whole data in ValueEventListener when something has changed. It gets only "delta", here is proof.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...