Shajeel Afzal
Shajeel Afzal

Reputation: 5953

FirebaseRecyclerAdapter not updating data automatically

I am using FirebaseRecyclerAdapter that comes with FirebaseAndroid-UI library and I assume that when the data will change on Firebase it will automatically refresh and update the RecyclerView.

My code is simple:

Firebase firebasePicturesRef = new Firebase(FIREBASE_URL);

mAdapter = new FirebaseRecyclerAdapter<Image, ImageViewHolder>(
        Image.class, R.layout.pictures_grid_item,
        ImageViewHolder.class, firebasePicturesRef) {

    @Override
    protected void populateViewHolder(ImageViewHolder viewHolder, Image model, int position) {
        super.populateViewHolder(viewHolder, model, position);
        Glide.with(getActivity()).load(model.getThumbnailUrl()).into(viewHolder.mImageView);
    }
};

I have also tried to manually refresh the adapter using notifyDatasetChanged in the the onDataChange method:

firebasePicturesRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (mAdapter != null)
            mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});

Am I doing something wrong? What is the fix?

Upvotes: 2

Views: 2098

Answers (1)

Shajeel Afzal
Shajeel Afzal

Reputation: 5953

I found the problem, i was calling mAdapter.cleanup(); in onStop() of the fragment. Removing it fixed the problem.

Instead of doing cleanup in onStop(); it should be called in onDestroy()

As suggested by Frank:

In general you should call cleanup() in the "opposite" method from where you create the adapter. So onCreate()<->onDestroy(), onStart()<->onStop(), onPause()<->onResume()

Upvotes: 4

Related Questions