Colton Idle
06/28/2022, 2:21 AMFrancesc
06/28/2022, 3:54 AMgildor
06/28/2022, 4:06 AMgildor
06/28/2022, 4:13 AMgildor
06/28/2022, 4:16 AMyou could have your own global scope that’s bound to the application lifecycle and inject that where neededThis solution is convinient, but careful, it may cause service to leak if you just use some global scope and do not cancel all your jobs on service destroy (because job which you running lifecycle may be longer than potential lifecycle of the service and will not be cancelled automatically)|
gildor
06/28/2022, 4:19 AMinject a scope factory that let’s you create a scope in your firebase class.Yes, it one of ways, but you should inject Factory and cancel created scope onDestroy, otherwise it will have the same issue as global scope (so in reality it’s pertty much the same as SO answer)
Colton Idle
06/28/2022, 5:47 PMColton Idle
06/28/2022, 7:21 PMgildor
06/29/2022, 1:59 AMdo “own” the service, but I need to extend from FirebaseMessagingServiceSo you do not own it, you extending it 😄
but I should be able to add an interface for LifecycleService.LifecycleService is not an interface, it’s a base class same way as ComponentActivity. So you probably just should follow SO
Colton Idle
06/29/2022, 2:10 AMgildor
06/29/2022, 2:15 AMEnhancedIntentService
as base class, which does exactly the same, creates executor with lifecycle of the service (shutting down it onDestroy)Ian Lake
06/29/2022, 2:36 AMLifecycleService
is just a shim wrapper around the actual logic in ServiceLifecycleDispatcher
, which is there exactly if you already have a base class for your service: https://developer.android.com/reference/androidx/lifecycle/ServiceLifecycleDispatchergildor
06/29/2022, 2:43 AMIan Lake
06/29/2022, 2:45 AMval coroutineContext
set up with something like SupervisorJob() + Dispatchers.Main
and coroutineScope.cancel()
in onDestroy()
, everything else is optional, tbhColton Idle
06/29/2022, 4:16 PM@AndroidEntryPoint
class MyFirebaseMessagingService : FirebaseMessagingService() {
@Inject lateinit var apiService: MyApiService
private val job = SupervisorJob()
private val scope = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO> + job)
//run some suspend functions when needed
override fun onDestroy() {
job.cancel()
super.onDestroy()
}
gildor
06/30/2022, 2:51 AMColton Idle
06/30/2022, 4:03 AM@AndroidEntryPoint
class MyFirebaseMessagingService : FirebaseMessagingService() {
@Inject lateinit var apiService: MyApiService
private val scope = CoroutineScope(Dispatchers.Main)
//run some suspend functions when needed
override fun onDestroy() {
scope.cancel()
super.onDestroy()
}
gildor
06/30/2022, 4:07 AMprivate val scope = CoroutineScope(Dispatchers.Main + SupervisorJob())
gildor
06/30/2022, 4:09 AMColton Idle
06/30/2022, 2:28 PM@AndroidEntryPoint
class MyFirebaseMessagingService : FirebaseMessagingService() {
@Inject lateinit var apiService: MyApiService
private val scope = CoroutineScope(Dispatchers.Main + SupervisorJob())
//run some suspend functions when needed
override fun onDestroy() {
scope.cancel()
super.onDestroy()
}
I learned so much. Thanks for teaching!Colton Idle
07/01/2022, 5:07 PMFrancesc
07/01/2022, 7:58 PM