hp2017
hp2017

Reputation: 61

With Android MVVM, how to avoid setting same data in recycler adapter again?

I need one basic clarification (maybe i’m missing the point). In All MVVM examples, (taking example of postViewModel)

    postViewModel.getAllposts().observe(this, new Observer<List<Post>>() {
            @Override
            public void onChanged(@Nullable List<Post> posts) {
                    Log.i(LOG_TAG,”posts data set changed, notifying “+posts);
                    postsAdapter.setPosts(posts);
            }
    });

And In,

    private void getPosts(List<Post> posts) {
            for(Post post:posts) {
                    postViewModel.insert(post);
            }
    }

So if there are three posts, for insert of each post, onChanged is called with all posts in DB. so first recycler is created with post 1, then second time, it’s set with posts 1 and 2 and at last, it’s set with posts 1,2 and 3. So View in recyclerlayout is fine, at the end it shows 3 items only. But it's not efficient if i get lot data which has many images. How do I avoid this ? Is there a way to pause the observe till all inserts are done ? Or to get only updated data in observe ?

Upvotes: 0

Views: 79

Answers (1)

Use DiffUtil to effectively update your RecyclerView. Here is an example

Upvotes: 3

Related Questions