From ace7923e2fa7916235a92b62e777c5fcaa800c13 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sun, 23 Jun 2024 11:59:21 +0530 Subject: [PATCH] Avoid some potential issues with blocking calls in suspend functions --- .../org/schabi/newpipe/database/feed/dao/FeedDAO.kt | 3 --- .../newpipe/database/subscription/SubscriptionDAO.kt | 11 ++++++++--- .../newpipe/local/subscription/SubscriptionManager.kt | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/database/feed/dao/FeedDAO.kt b/app/src/main/java/org/schabi/newpipe/database/feed/dao/FeedDAO.kt index 32665ebcb..ec6e2b4e6 100644 --- a/app/src/main/java/org/schabi/newpipe/database/feed/dao/FeedDAO.kt +++ b/app/src/main/java/org/schabi/newpipe/database/feed/dao/FeedDAO.kt @@ -140,9 +140,6 @@ interface FeedDAO { ) suspend fun unlinkOldLivestreams(subscriptionId: Long) - @Insert(onConflict = OnConflictStrategy.IGNORE) - fun insert(feedEntity: FeedEntity) - @Insert(onConflict = OnConflictStrategy.IGNORE) suspend fun insertAll(entities: List): List diff --git a/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionDAO.kt b/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionDAO.kt index 10f2dfc22..56ba2038e 100644 --- a/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionDAO.kt +++ b/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionDAO.kt @@ -6,12 +6,17 @@ import androidx.room.OnConflictStrategy import androidx.room.Query import androidx.room.RewriteQueriesToDropUnusedColumns import androidx.room.Transaction +import androidx.room.Update import io.reactivex.rxjava3.core.Flowable import io.reactivex.rxjava3.core.Maybe import org.schabi.newpipe.database.BasicDAO @Dao interface SubscriptionDAO : BasicDAO { + // TODO: Replace with the standard update method when migrating to suspend functions + @Update + suspend fun updateSubscription(entity: SubscriptionEntity) + @Query("SELECT COUNT(*) FROM subscriptions") fun rowCount(): Flowable @@ -84,10 +89,10 @@ interface SubscriptionDAO : BasicDAO { fun deleteSubscription(serviceId: Int, url: String): Int @Query("SELECT uid FROM subscriptions WHERE url LIKE :url AND service_id = :serviceId") - fun getSubscriptionIdInternal(serviceId: Int, url: String): Long? + suspend fun getSubscriptionIdInternal(serviceId: Int, url: String): Long? @Insert(onConflict = OnConflictStrategy.IGNORE) - fun silentInsertAllInternal(entities: List): List + suspend fun silentInsertAllInternal(entities: List): List @Transaction suspend fun upsertAll(entities: List): List { @@ -103,7 +108,7 @@ interface SubscriptionDAO : BasicDAO { ?: throw IllegalStateException("Subscription cannot be null just after insertion.") entity.uid = subscriptionIdFromDb - update(entity) + updateSubscription(entity) } } diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionManager.kt b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionManager.kt index a206b8477..0af097f5b 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionManager.kt +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionManager.kt @@ -112,7 +112,7 @@ class SubscriptionManager(context: Context) { info.description?.let { subscriptionEntity.description = it } info.subscriberCount?.let { subscriptionEntity.subscriberCount = it } - subscriptionTable.update(subscriptionEntity) + subscriptionTable.updateSubscription(subscriptionEntity) } fun deleteSubscription(serviceId: Int, url: String): Completable { @@ -142,7 +142,7 @@ class SubscriptionManager(context: Context) { .map { channel -> channel.relatedItems.filterIsInstance().map { stream -> StreamEntity(stream) } } .flatMapCompletable { entities -> Completable.fromAction { - runBlocking { database.streamDAO().upsertAll(entities) } + database.streamDAO().upsertAll(entities) } }.onErrorComplete() }