Reputation: 3090
I want to remove some data from my realtime database when the app is going to close.
I defined the function like this (will be adding logic to it later):
exports.removeAllAssignedTeams = functions.https.onCall((data, context) => {
const judgeId = data.judgeId;
console.log("Will unassign all teams assigned to judge id: ", judgeId);
});
And I am calling it from my app delegate:
func applicationWillTerminate(_ application: UIApplication) {
print("Application will terminate")
functions.httpsCallable("removeAllAssignedTeams").call(["judgeId": getUserPhoneNumber()]) { (_, _) in }
}
But in my cloud functions console, I never see any logs for this function or even an indication that the function started execution. What am I doing wrong?
Upvotes: 0
Views: 193
Reputation: 317487
I suspect that you're not doing anything wrong. It's more likely that the application process is terminating before the request actually hits the function. According to the documentation for applicationWillTerminate:
Your implementation of this method has approximately five seconds to perform any tasks and return. If the method does not return before time expires, the system may kill the process altogether.
I suggest testing your code outside of applicationWillTerminate to see if it works the way you expect. If it does, then you can assume that it's just not finishing in time for the function to be invoked before the process dies.
Also keep in mind that invoking a callable function is asynchronous, and call()
will return immediately before the result is received. Your implementation is therefore returning immediately, which is probably signaling to iOS that you're done with your work. Even if you manage to make your implementation block until the call is complete, it still might not finish before your approximate 5 seconds are up.
Upvotes: 1
Reputation: 599041
If you want to remove data from the Realtime Database when the user disconnects, consider using an onDisconnect
handler. With such a handler, the server will remove the data when is detects that the client is gone. This may take a few minutes (depending on how the app is closed), but will typically be more reliable than trying to do any network calls while the app is closing as the instruction on what to do is sent to the server straight away when you call onDisconnect
.
If you're using an onDisconnect
handler, what you can do in the applicationWillTerminate
is to actively tell the client to close the connection by calling the goOffline()
method. While this also has a chance of not working, in that case the server will still detect that the client is gone a few minutes later, while in the case where the call to go offline makes it through, the onDisconnect
handler will be executed straight away.
If the code to remove the data is complex, consider having a simple value in the database that you remove with an onDisconnect
handler. By then attaching a Cloud Function to the removal of that simple value, the Cloud Functions code will run when the server has detected that the client is gone.
Upvotes: 0