I'm working on a codebase with both Java and Kotlin code, i introduced Kotlin coroutines to help in running background tasks, however i have a function in my repository marked with the keyword suspend and i call it from an Intent Service that is written in java to sync local db with remote. However i can only call a suspend function from another suspend function or a coroutine builder. I three options in mind:
Option 1
Use
GlobalScope.launch(Dispatchers.IO) on the function so that i can call it from the Intent Service. However GloablScope is not highly recommended in the android context.
Option 2
Make my repository extend Coroutine Scope, create a public function called onCleared that cancels job, then call this function when the service onDestroyed method is called or when the view model onCleared method is called. This option is to try and cancel any running operations.
Option 3
Use the viewmodel scope to launch the coroutine in the repository, then call the viewmodel from the service, however the viewmodel should only be limited to an activity or fragment.
Option 4
Refactor the intent service to Kotlin and use coroutines