https://kotlinlang.org logo
#android
Title
# android
d

dave08

11/24/2019, 5:23 PM
It seems like
runBlocking {}
isn't possible anymore, since you can't use the
lifecycleScope
with it...
l

louiscad

11/25/2019, 1:20 PM
Using
runBlocking
on Android for something other than tests is almost always a bad idea.
d

dave08

11/25/2019, 1:35 PM
I have a SyncAdapter with this:
Copy code
override fun onPerformSync(
			account: Account,
			bundle: Bundle,
			s: String,
			contentProviderClient: ContentProviderClient,
			syncResult: SyncResult
	) = coroutineScope.launch {

		syncEventTrigger.onSyncStart(context, syncResult)
				.onStart { i { "Sync Started" } }
				.onCompletion { i { "Sync Stopped" } }
				.collect()
    }.let { Unit }
But I don't want the onPerformSync to exit until the
collect
finishes...
I'm assuming that since the `Service`'s
lifecycleScope
is using
Dispatchers.Main.immediate
, that it will block the service until the sync is finished. Am I right?
But then, maybe that's a bit flaky to count on such things... so
runBlocking
might be better here...
l

louiscad

11/25/2019, 3:50 PM
You're not right, the service will not be "blocked". It'll stay there until you ask it to stop or the user or Android stops it.
d

dave08

11/25/2019, 3:52 PM
That's for a regular service, but a SyncAdapter binds to a service, so doesn't the service get stopped when Android unbinds it at the end of the Sync?
l

louiscad

11/25/2019, 7:18 PM
You can check the sources of
LifecycleService
from your IDE to find out, and be sure.