igor.wojda
09/15/2023, 7:16 AMonReceive
? Is there any better way of doing it than this?
override fun onReceive(context: Context, intent: Intent) {
val receivedData = intent.getIntExtra(DATA, DEFAULT_DATA)
GlobalScope.launch {
val receivedDataId = database.insertNewMessage(receivedData)
}
}
Jakub Syty
09/15/2023, 7:21 AMGlobalScope
is never the good answer 😄 Just create a new scope as property of the receiver class. Like private val scope = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>)
Wojtek Kalicinski
09/15/2023, 8:29 AMlaunch
returns immediately and once you exit onReceive
there's potentially nothing to keep your BroadcastReceiver or your app alive.
You could use goAsync
if you absolutely must do (async) work in onReceive, but ideally you should offload the work to a component that is better suited for itigor.wojda
09/15/2023, 8:55 AMJasurbek Abdiroziqov
09/15/2023, 9:15 AMSwapnil Musale
09/15/2023, 1:08 PM