1

I need to save the last document snapshot from firestore query. I have tried to save this Document snaphot to shared preference on Android, but I I always get crash.

I try to save and get the Document Snapshot using these methods below, from this store and retrieve a class object in shared preference:

fun saveLastDocumentAttendedEvents(lastDocument: DocumentSnapshot) {

        val json = gson.toJson(lastDocument)
        with(sharedPref.edit()) {
            putString("LAST_DOCUMENT_ATTENDED_EVENTS", json)
            apply()
        }

    }


    fun loadLastDocumentAttendedEvents() : DocumentSnapshot?  {
        val json = sharedPref.getString("LAST_DOCUMENT_ATTENDED_EVENTS", "")
        return gson.fromJson(json, DocumentSnapshot::class.java)
    }

I usually can save custom object to shared preference without problem using those methos, But it always crash if saving DocumentSnaphot to shared preference with error message:

at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97) 2019-09-29 11:35:07.036 15370-15370/com.muchammadagunglaksana.kumpul_muslim E/AndroidRuntime: at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)

I need to save it on shared preference, file or etc to persist the last document from firestore query

Alexa289
  • 8,089
  • 10
  • 74
  • 178
  • The stack track you've shown looks incomplete. We can't see what type of exception it is, along with its message. Please edit the question to show more completely the error. Also please show the contents of the document. – Doug Stevenson Sep 29 '19 at 06:54
  • Please add what Doug Stevenson asked for and please also responde with @. – Alex Mamo Sep 29 '19 at 10:00

1 Answers1

1

Ideally instead of implementing your own cache you can use firebase's own caching system.

To enable it you should use

val settings = FirebaseFirestoreSettings.Builder()
        .setPersistenceEnabled(true)
        .build()
db.firestoreSettings = settings

By Enabling this even if your app is offline, firebase would return the last processed result for that query.

Even despite this if you want to use your own caching system I would recommend getting the value from the sanpshot class and saving that instead of saving the whole snapshot.

Example here Kotlin Gson Deserializing

Ninja420
  • 3,542
  • 3
  • 22
  • 34
  • 1
    While this is true, it doesn't really answer the question. – Doug Stevenson Sep 29 '19 at 06:53
  • I am trying to use firebase caching system first. it seems more appropriate. but I don't know how to use it for my case, could you please help me if you don't mind https://stackoverflow.com/questions/58160475/how-to-force-to-get-cached-documents-in-firestore-i-want-to-get-cached-documen – Alexa289 Sep 30 '19 at 01:22