Firebaseui-android: Access to DocumentSnapshots in FirestoreRecyclerAdapter

Created on 6 Oct 2017  路  9Comments  路  Source: firebase/FirebaseUI-Android

Hey guys, loving the new Firestore stuff. Quick question. I am converting from RTD to this and one thing I did in my previous schema was to not explicitly store the key inside the "object" because I could explicitly use getRef(position) to grab the key to attach to my local POJO. It seems that this automatic reference is not there to grab the id of the DocumentSnapshot.

I am able to manage it myself by overriding onChildChanged()...

@Override
    public void onChildChanged(ChangeEventType type, DocumentSnapshot snapshot, int newIndex, int oldIndex) {
        super.onChildChanged(type, snapshot, newIndex, oldIndex);
        //....use a local variable to store metadata, document id, etc from the DocumentSnapshot by the newIndex
    }

But I wanted to see if this was the best way to access the document id, metadata, etc, or if there was something else.

Thanks!!!!

All 9 comments

Try this: FirestoreRecyclerAdapter.getSnapshots()#getSnapshot(index).

@SUPERCILEX that gives me the Object I passed into the FirestoreRecyclerAdapter, which is built off the document information. Unless there is a way, in that POJO to tell the class to pull key or metadata and store in the object, it doesn't seem it would work??

I'm not sure I understand, that method gives you a DocumentSnapshot. Is that not what you want?

It appears the way I am using FirestoreRecyclerAdapter it gives me back the Object I instantiate
the adapter with:

inventoryViewAdapter = new FirestoreRecyclerAdapter<Item, InventoryViewAdapter.ViewHolder>(options) {
......

provides the following when I try to get a DocumentSnapshot from getSnapshots:

screenshot 2017-10-06 10 56 54

@SUPERCILEX nevermind, I read your response closer and got it

DocumentSnapshot r = getSnapshots().getSnapshot(position);

works!

Awesome!

@bagintz Looks like we can close this. Thanks @SUPERCILEX

I had been searching the docs for a way to get the document id, I'm glad I finally the answer but this info should be in the docs! Unless I completely missed it, which in that case I apologize.

I agree with @kr05. In my case I wanted to get the snapshot to recognize if it had pending writes before processing:

val r = snapshots.getSnapshot(position)
                if (r.metadata.hasPendingWrites()) {
                    return
                }

Thankfully I stumbled upon this thread after a bit of searching.

Was this page helpful?
0 / 5 - 0 ratings