1

I am Using Room Persistent Data of Android, to store data coming from the server - locally. In addition, I am using MVVM pattern, recommended by Android official docs, thus my recyclerView gets displayed inside a LiveData Observer something like this:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.activity_feed, container, false);

    ((App)getActivity().getApplication()).getmAppComponent().inject(this);

    rvFeed = view.findViewById(R.id.rvFeed);
    mLinearLayoutManager = new LinearLayoutManager(getActivity());
    rvFeed.setLayoutManager(mLinearLayoutManager);
    feedAdapter = new FeedAdapter(this);
    feedAdapter.setRV(rvFeed);

    mFactory = new PostsViewModelFactory(postRepository);
    mViewModel = ViewModelProviders.of(this, mFactory).get(PostsViewModel.class);
    mViewModel.init();
    mViewModel.getPostsLiveData().observe(this, new Observer<List<Post>>() {
        @Override
        public void onChanged(@Nullable List<Post> posts) {

            feedAdapter.setPostsList(posts);

            if (firstLaunch) {
                rvFeed.setAdapter(feedAdapter);
                firstLaunch = false;
            }
            loading = true;
        }
    });
    return view;
} 

The mViewModel.getPostsLiveData() is bassically retrieving LiveData from Room (via repository), thus, any new data added to Room triggers the LiveData Observer, and the feedAdapter repopulates the recyclerView(i.e. setPostsLists() which in return calls notifyDataSetChanged()).

But as you can notice the new fresh data added to room won't be displayed as first item in the recyclerView since it doesn't get added to the Adapter directly [hence, I cannot call notifyItemInserted(0)], but rather to Room (local storage).

So my question is, what's the best way to add to the local storage (Room) but in addition to force the item to be displayed as first in the recyclerView when it get's re populated when the LiveData observer get's triggered?

Thanks in advance :)

Hudi Ilfeld
  • 1,905
  • 2
  • 16
  • 25
  • `the new fresh data added to room won't be displayed as first item in the recyclerView since it doesn't get added to the Adapter directly` - could you please elaborate ? To me, it looks like `observe` will be called and the item will be added. – Anatolii Sep 02 '18 at 09:30
  • @Anatolii thanks for the quick response. the new data coming from Room is stored as the last item in `sqlite`. hence when I repopulate the recyclerView it displays the Room data in it's order. which means the newest data gets displayed as the last. am I missing something? – Hudi Ilfeld Sep 02 '18 at 09:55
  • if `List posts` is all the data from `sqlite` then why can't you just reverse the list to display items in the reverse order? – Anatolii Sep 02 '18 at 10:11
  • @Anatolii That could be a could option, the problem is that I am using an endless recyclerView. that is; when I scroll to the bottom and load new data , if I reverse the list then the new data will be added at the top of the recyclerView instead of the bottom. – Hudi Ilfeld Sep 02 '18 at 20:10

0 Answers0