From 273393f68c662b9616b68a3fd0227d111193dcae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mark=20Hu=CC=88sers?= Date: Thu, 2 Nov 2023 14:42:01 +0100 Subject: [PATCH] chore: Switch to Dispatcher.limitedParallelism The documentation of the newSingleThreadContext recommends the usage of Dispatcher.limitedParallelism in our case. --- .../subscription/SpecificationPropertyListener.kt | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/subscription/SpecificationPropertyListener.kt b/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/subscription/SpecificationPropertyListener.kt index cd4770b1..7282888b 100644 --- a/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/subscription/SpecificationPropertyListener.kt +++ b/kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/subscription/SpecificationPropertyListener.kt @@ -20,11 +20,9 @@ package org.eclipse.kuksa.subscription import android.util.Log -import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.newSingleThreadContext import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.withContext import org.eclipse.kuksa.PropertyListener import org.eclipse.kuksa.VssSpecificationListener import org.eclipse.kuksa.extension.TAG @@ -48,15 +46,14 @@ internal class SpecificationPropertyListener( // Multiple onPropertyChanged updates from different threads may be called. The updatedVssSpecification must be // in sync however. Calling the .copy in a blocking context is necessary for this. - @OptIn(ExperimentalCoroutinesApi::class, DelicateCoroutinesApi::class) - private val specificationUpdateContext = newSingleThreadContext("SpecificationUpdateContext") + @OptIn(ExperimentalCoroutinesApi::class) + private val specificationUpdateContext = Dispatchers.IO.limitedParallelism(1) override fun onPropertyChanged(vssPath: String, field: Types.Field, updatedValue: Types.DataEntry) { Log.d(TAG, "Update from subscribed property: $vssPath - $field: ${updatedValue.value}") - runBlocking { - withContext(specificationUpdateContext) { - updatedVssSpecification = updatedVssSpecification.copy(vssPath, updatedValue.value) - } + + runBlocking(specificationUpdateContext) { + updatedVssSpecification = updatedVssSpecification.copy(vssPath, updatedValue.value) } initialSubscriptionUpdates[vssPath] = true