Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

java - Deleting abandoned firestore documents, by keeping a timer running after the app is killed

I'm working on an app where the user creates a party where people can join, and all parties are stored in a Firestore collection as documents.

What I want to do is that when the user leaves the app after creating a party, he gets 5m, after 5m the party he created gets deleted from the cloud firestore. So basically, I want to remove the inactive firestore documents(parties) as soon as possible.

I tried to make a timer, when he doesn't kill the app, the timer works well and deletes the document after 5m.

The problem is that when he kills the app, the app obviously stops, so does the timer, which prevents me from deleting his party. What should I do? Is there a way to make the timer keeps ticking even after killing the app? The timer is just a solution I thought of to delete the abandoned parties, other suggestions are welcomed.

I tried to make an explicit broadcast which is triggered in onStop, and I start the timer in onRecieve, but onRecieve stops when the app is killed, to make it run when the app is killed I need to trigger it after the app is killed, but I don't know how to do this.

Note: Solutions other than cloud functions are preferable. But if there is no other way please inform me.

question from:https://stackoverflow.com/questions/65838006/deleting-abandoned-firestore-documents-by-keeping-a-timer-running-after-the-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do this by using a WorkManager, you follow the steps below for implementing it.

  1. Create a WorkManager and extend it with CoroutineWorker instead of a Worker.
  2. Write the code for deleting the parties in the doWork() function of that Work Manager.
  3. Create a one time Work Request using OneTimeWorkRequestBuilder with that Work Manager.
  4. Finally, you need to submit your WorkRequest to WorkManager using the enqueue() method at the time when you are starting that timer.
  5. Don't forget to setInitialDelay() which will be like a timer for the Work Manager. After implementing this, you can also remove the timer from the app.
  6. This method will work in both conditions when the app is background or foreground.

For example, you can also check: https://developer.android.com/topic/libraries/architecture/workmanager/basics


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...