Gene Bo
Gene Bo

Reputation: 12063

Callbacks for FirebaseRecyclerAdapter not running

Going through the Code Lab for Firebase Android Friendly Chat - at https://codelabs.developers.google.com/codelabs/firebase-android/ - I am running into an issue where none of the callbacks are being reached/called for step 7: Read Messages

Going through the /android-start project, I am able to get through the first steps 2-6:

  1. Overview
  2. Get the sample code
  3. Import the starter app
  4. Create Firebase console Project
  5. Run the starter app
  6. Enable Authentication (android app added with package name and SHA1 via project console, etc)

However, when I go to do step 7 - none of the callbacks there get called. I run the app and the sign-in/out steps are working.

I have imported initial_messages.json as instructed there and copied over the code exactly for step 7 (several attempts in an effort to make sure maybe I didn't miss something along the way).. and then even tried in the finished /android project. There also, the same issue where callbacks never get run and so the spinner just hangs there.

Since there's so much code, I'll post the sections where I am expecting some callback to run. Can anyone please advise on what I might be missing here? And no problem - if it will help to add more of the code I have. Thanks for reading

    // mProgressBar.setVisibility(ProgressBar.INVISIBLE);
    // New child entries
    mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();

    SnapshotParser<FriendlyMessage> parser = new SnapshotParser<FriendlyMessage>() {
        @Override
        public FriendlyMessage parseSnapshot(DataSnapshot dataSnapshot) {
            ... 
        }
    };
    ...

    DatabaseReference messagesRef = mFirebaseDatabaseReference.child(MESSAGES_CHILD);

    ... 

    FirebaseRecyclerOptions<FriendlyMessage> options =
            new FirebaseRecyclerOptions.Builder<FriendlyMessage>()
                    .setQuery(messagesRef, parser)
                    .build();

    mFirebaseAdapter = new FirebaseRecyclerAdapter<FriendlyMessage, MessageViewHolder>(options) {
        @Override
        public MessageViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            ... 
        }

        @Override
        protected void onBindViewHolder(final MessageViewHolder viewHolder,
                                        int position,
                                        FriendlyMessage friendlyMessage) {
            ....

        }
    };

    mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            ...
        }
    });


    mMessageRecyclerView.setAdapter(mFirebaseAdapter);

Upvotes: 5

Views: 554

Answers (1)

Grimthorr
Grimthorr

Reputation: 6926

In version 3.0 of FirebaseUI, a new lifecycle policy for FirebaseRecyclerAdapter was introduced. Therefore, you are now required to explicitly call startListening() & stopListening() on the adapter to instruct it to start & stop retrieving data from the database.

From the FirebaseUI 3.0 upgrade guide:

Adapter Lifecycle - in previous versions the adapters began listening immediately upon instantiation and had a cleanup() call to stop listening. In 3.x you must explicitly call startListening() and stopListening() or pass a LifecycleOwner to the options builder.

This is a very recent release and it looks like the Code Lab hasn't yet been updated to reflect this breaking change. I have raised this as an issue on the firebase/friendlychat-android GitHub repo.

Upvotes: 3

Related Questions