@@ -32,7 +32,6 @@ import at.bitfire.davdroid.repository.DavServiceRepository
3232import dagger.Lazy
3333import dagger.hilt.android.qualifiers.ApplicationContext
3434import kotlinx.coroutines.Dispatchers
35- import kotlinx.coroutines.runBlocking
3635import kotlinx.coroutines.runInterruptible
3736import kotlinx.coroutines.withContext
3837import okhttp3.HttpUrl
@@ -92,7 +91,7 @@ class PushRegistrationManager @Inject constructor(
9291 else
9392 UnifiedPush .unregister(context, serviceId.toString())
9493
95- // UnifiedPush has now been called. It will do its work and then call back to UnifiedPushService, which
94+ // UnifiedPush has now been called. It will do its work and then asynchronously call back to UnifiedPushService, which
9695 // will then call processSubscription or removeSubscription.
9796 }
9897
@@ -158,13 +157,13 @@ class PushRegistrationManager @Inject constructor(
158157 /* *
159158 * Called when no subscription is available (anymore) for the given service.
160159 *
161- * Unsubscribes from all collections.
160+ * Unsubscribes from all subscribed collections.
162161 */
163- internal suspend fun removeSubscription (serviceId : Long ) {
164- val service = serviceRepository.get(serviceId) ? : return
162+ internal suspend fun removeSubscription (serviceId : Long ) = withContext(dispatcher) {
163+ val service = serviceRepository.get(serviceId) ? : return @withContext
165164 val unsubscribeFrom = collectionRepository.getPushRegistered(service.id)
166165 if (unsubscribeFrom.isEmpty())
167- return
166+ return @withContext
168167
169168 val account = accountRepository.get().fromName(service.accountName)
170169 httpClientBuilder.get()
@@ -268,10 +267,8 @@ class PushRegistrationManager @Inject constructor(
268267 *
269268 * Otherwise, a potentially existing worker is cancelled.
270269 */
271- fun updatePeriodicWorker () {
272- val workerNeeded = runBlocking {
273- collectionRepository.anyPushCapable()
274- }
270+ suspend fun updatePeriodicWorker () = withContext(dispatcher) {
271+ val workerNeeded = collectionRepository.anyPushCapable()
275272
276273 val workManager = WorkManager .getInstance(context)
277274 if (workerNeeded) {
@@ -301,6 +298,9 @@ class PushRegistrationManager @Inject constructor(
301298 private const val WORKER_UNIQUE_NAME = " push-registration"
302299 const val WORKER_INTERVAL_DAYS = 1L
303300
301+ /* *
302+ * Single-thread dispatcher to synchronize tasks.
303+ */
304304 val dispatcher = Dispatchers .IO .limitedParallelism(1 )
305305
306306 }
0 commit comments