dave08
02/13/2020, 1:41 PMrunBlocking { }
in a `SyncAdapter`'s onPerformSync
or in a Service
will crash the application with an InterruptedException
(as in https://github.com/Kotlin/kotlinx.coroutines/blob/9be85a44f157cea6bf14b685a87c6709ebd9664b/kotlinx-coroutines-core/jvm/src/Builders.kt#L27 )? What does everyone do about this? Surround it with a runCatching { runBlocking { } }
🙈?satyan
02/13/2020, 1:48 PMrunBlocking
means:
Runs a new coroutine and **blocks** the current thread _interruptibly_ until its completion.
Do you really need to block the current thread for your operation ?
Can’t you use a coroutine scope (scoped to service lifecycle) to run your job ?dave08
02/13/2020, 1:51 PMWorkManager
Worker
...override fun onPerformSync(
account: Account,
bundle: Bundle,
s: String,
contentProviderClient: ContentProviderClient,
syncResult: SyncResult
) = runBlocking {
i { "Running sync..." }
val workManager = WorkManager.getInstance(context)
val syncRequest = OneTimeWorkRequestBuilder<SyncWork>()
.build()
workManager.enqueueUniqueWork(
SYNC_WORK_UNIQUE_NAME, ExistingWorkPolicy.APPEND, syncRequest
).await()
syncResult += workManager.getWorkInfosByTagLiveData(SYNC_WORK_UNIQUE_NAME)
.asFlow().onEach {
d { "WorkInfo (${syncRequest.id}): $it" }
}.singleOrNull()?.firstOrNull { it.state.isFinished }?.outputData
Unit
}
louiscad
02/16/2020, 10:32 AMdave08
02/17/2020, 3:11 AM