I am currently developing an app that can keep track of incoming calls on a device and save the caller's name and phone number to a database. However, I'm facing an issue with how to store the data from the Broadcast Receiver to the Room DB. My project uses MVVM architecture and dependency injection, so I injected a repository class in the Broadcast Receiver. To save data in the DB, I'm using coroutines to perform asynchronous work. However, I'm unsure how to cancel the job of each coroutine used in the Broadcast Receiver.
I have added a method in the broadcast receiver to cancel the coroutine jobs.
fun clearJob(){ job?.cancel() }
I called clearJob method before unregisterting the broadcast receiver like this.
override fun onDestroy() { incomingCallReceiver.clearJob() unregisterReceiver(incomingCallReceiver) super.onDestroy() }