0

I want to update the data in Firebase when the application terminates. How can I do that

I used onDestroy and onPause but they did not do the function I wanted. When I use OnPause, every time the application goes to the background, it does the update operation, and I don't want that either.

// This onDestroy  didn't work OnPause, every time the application goes to the background

override fun onDestroy() {
    super.onDestroy()
    Log.d("ClosingService", "Service is destroyed. Updating user data...")
    Update_User_Data(dataManager.userDataList)
}


 fun Update_User_Data(newDataList : ArrayList<User_Structure>){
            val tasks = newDataList.map { i ->
            val userRef = db.collection("users").document(i.userId)
            userRef.update(mapOf(
                "username" to i.username,
                "favorites" to i.favorites,
                "parts" to i.parts,
                "points" to i.points
            ))
            }
            Tasks.whenAllComplete(tasks).addOnCompleteListener {
                if (it.isSuccessful) {
                    println("All user data updated successfully!")
                } else {
                    println("Error updating some user data.")
                }
            }
    }
5
  • 1
    "user closes the application" -- what is your definition of this? Commented Jul 5 at 15:26
  • 1
    Please update the question with an explanation of why you are trying to do this, or the specific problem you're trying to solve. It could be the case that what you're trying either isn't truly possible, or isn't advisable at all, or isn't even necessary. We don't know because we don't understand the use case you're trying to implement. Commented Jul 5 at 15:43
  • I want to update the data in firebase when the application terminates
    – Umut.jpg
    Commented Jul 5 at 16:14
  • 3
    There's no guarantee that onDestroy() gets called -- an app may terminate "suddenly" for a number of reasons. You may want to look at something like onSaveInstanceState() instead.
    – MarsAtomic
    Commented Jul 5 at 18:41
  • @MarsAtomic Thank you, I did as you said, thank you for your help.
    – Umut.jpg
    Commented Jul 6 at 13:17

1 Answer 1

0

I used OnDestroy and OnPouse but they did not do the function I wanted.

Please bear in mind, that the onDestroy is not always called. So you cannot rely on that. As @MarsAtomic already mentioned in his comment, you should consider using onSaveInstanceState() instead which is:

Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle).

1
  • Thanks, I did what you said and set it to send requests only when the data object is updated. If I have a problem, this time I will save the favorites locally, perform the favorite word operations there, and set the other data to be updated after the test solutions.
    – Umut.jpg
    Commented Jul 6 at 13:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.