Hey what is the desired way to launch coroutine in...
# android
i
Hey what is the desired way to launch coroutine in the
onReceive
? Is there any better way of doing it than this?
Copy code
override fun onReceive(context: Context, intent: Intent) {
    val receivedData = intent.getIntExtra(DATA, DEFAULT_DATA)

    GlobalScope.launch {
            val receivedDataId = database.insertNewMessage(receivedData)
        }
}
j
GlobalScope
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>)
❤️ 1
w
Whichever scope you end up using, this has other problems, namely that
launch
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 it
i
I get the idea, and I though worker will be enough, but thus is not the case, due to 10kb limit, so I am looking for a good alternative on how to pass data from onReceive to Worker
j
You need to pass params only and inside WM you could decide what to do based on params you receive
s
And if you concerned about WorkManager payload size to pass into it then use Local Database to save and retrive data inside WM