1

Every time the app has crashed after update some doc fields. These fields updated as expected, but crash comes in callback addOnSuccessListener. When I remove OnSuccessListener, this works fine. Any suggestions?

Upd: I've figured out that all my Firestore calls makes the app crash in addOnSuccessListener, so the reason is more global. Also using addOnCompleteListener instead of addOnSuccessListener works as expected.

Upd2: Issue discussion and solution

fun setAppointmentStatus(docId: String, status: String, callback: (succeed: Boolean) -> Unit) {    
        db.collection(path)
                .document(docId)
                .update("status", status, "updatedAt", FieldValue.serverTimestamp())
                .addOnSuccessListener {
                    callback(true)
                }
}

Stack trace:

2022-01-03 18:25:50.654 9378-9378/com.project.provider E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.project.provider, PID: 9378
    java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter it
        at com.biosensics.nula.cloud.AppointmentCloudHelper$setAppointmentStatus$1.onSuccess(Unknown Source:2)
        at com.biosensics.nula.cloud.AppointmentCloudHelper$setAppointmentStatus$1.onSuccess(Unknown Source:2)
        at com.google.android.gms.tasks.zzm.run(com.google.android.gms:play-services-tasks@@18.0.0:1)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Konstantin Konopko
  • 5,229
  • 4
  • 36
  • 62

1 Answers1

0

To achieve what you are trying to do, that seems to be to add a timestamp to your document, you can do as follows:

// If you're using custom Kotlin objects in Android, add an @ServerTimestamp
// annotation to a Date field for your custom object classes. This indicates
// that the Date field has to be treated as a server timestamp by the object mapper.
val docRef = db.collection("objects").document("some-id") 

// Update the timestamp field with the value from the server
val updates = hashMapOf<String, Any>(
        "timestamp" to FieldValue.serverTimestamp()
)

docRef.update(updates).addOnCompleteListener { }

You only need to substitute the required variables of your own app. Changing “objects” to path, and “some-id” to docId.


To Get File Metadata you can try this:

// Create a storage reference from our app
val storageRef = storage.reference

// Get reference to the file
val forestRef = storageRef.child("images/forest.jpg")

forestRef.metadata.addOnSuccessListener { metadata ->
    // Metadata now contains the metadata for 'images/forest.jpg'
}.addOnFailureListener {
    // Uh-oh, an error occurred!
}

And if you want to learn more about tasks, I recommend reading this blog entry and it’s kotlin example where adding OnFailureListener is recommended to get the specific exception as a NPE can have a variety of causes.

Alex
  • 778
  • 1
  • 15