TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
220 stars 49 forks source link

Firestore: Collection SnapshotListener #103

Closed SeriousM closed 1 year ago

SeriousM commented 1 year ago

Hi!

When I use ICollectionReference.AddSnapshotListener<T>(onChange => {...}) the onChange contains two collections:

How do I get the document to the document change like

Thanks!

TobiasBuchholz commented 1 year ago

Hi, the DocumentChange object did include indeed a reference to the IDocumentSnapshot interface which was rather useless. I've created a new version 1.2.5 of the plugin which references the IDocumentSnapshot<T> interface instead, so you can now use its Data property to access your data regarding to the change like this:

onChanged.DocumentChanges.First().DocumentSnapshot.Data

The GetDocumentChanges() method returns an enumerable of the documents that changed since the last snapshot. You can pass true or false to it to include metadata-only changes, so I think that's why the method exists.

You can take a look at the official firebase docs for additional information. Furthermore I've incooperated this new code at the gets_real_time_updates_on_multiple_documents() test method of the FirestoreFixture, so you can take a look there as well.

https://github.com/TobiasBuchholz/Plugin.Firebase/blob/97e254870ea7765eed20eaaac51c4fa3b44d79dd/tests/Plugin.Firebase.IntegrationTests/Firestore/FirestoreFixture.cs#L306

SeriousM commented 1 year ago

Hi Tobias, thank you again for your excellent support and all that information. I will check this new version tomorrow. You're work is very appreciated!