Android Kotlin – Management Offline Firestore Data & Automatically Sync It

You may also like...

7 Responses

  1. Dutch Masters says:

    I posted a bounty on stackoverflow about offline with Firestore and your blog answers it.
    https://stackoverflow.com/questions/47843218/offline-issue-with-firestore-vs-firebase

    post an answer there and i’ll award you

  2. Tom says:

    It seems like the code has a potential memory leak, unless I don’t understand the code correctly (I don’t know Kotlin only Java).

    When calling the method .addSnapshotListener() it returns a ListenerRegistration object. https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/DocumentReference

    My understanding is that if you’ve got a listener on something that it is going to remain in memory until it is removed so if you have a bunch of .addSnapshotListener() calls it will just continue to build all of those up in memory. Is that correct or am I missing something?

    • Dutch Masters says:

      Tom,
      That is a good observation.. there is a second form of adding the listener..
      public ListenerRegistration addSnapshotListener (Activity activity, DocumentListenOptions options, EventListener listener)
      In this form, the listener is scoped to the activity, and will automatically be removed when the Activity.onStop() is called. You can also get a ListenerRegistration and handle the unsubscribe yourself.

      There is another potential pitfall with this simplified example: The listener can get called twice, because there are two events in the lifecycle: one where the data is synced locally, and one where it is synced remotely. To tell which it is, check SnapshotMetadata.isFromCache().

      There is another potential pitfall with this technique.

  1. November 28, 2017

    […] Update offline data & sync it here […]

  2. November 28, 2017

    […] Update offline data & sync it here […]

  3. November 28, 2017

    […] Update offline data & sync it here […]

Leave a Reply

Your email address will not be published. Required fields are marked *