1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-07-03 08:08:48 +00:00

Avoid some potential issues with blocking calls in suspend functions

This commit is contained in:
Isira Seneviratne 2024-06-23 11:59:21 +05:30
parent dac292181d
commit ace7923e2f
3 changed files with 10 additions and 8 deletions

View File

@ -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<FeedEntity>): List<Long>

View File

@ -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<SubscriptionEntity> {
// 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<Long>
@ -84,10 +89,10 @@ interface SubscriptionDAO : BasicDAO<SubscriptionEntity> {
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<SubscriptionEntity>): List<Long>
suspend fun silentInsertAllInternal(entities: List<SubscriptionEntity>): List<Long>
@Transaction
suspend fun upsertAll(entities: List<SubscriptionEntity>): List<SubscriptionEntity> {
@ -103,7 +108,7 @@ interface SubscriptionDAO : BasicDAO<SubscriptionEntity> {
?: throw IllegalStateException("Subscription cannot be null just after insertion.")
entity.uid = subscriptionIdFromDb
update(entity)
updateSubscription(entity)
}
}

View File

@ -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<StreamInfoItem>().map { stream -> StreamEntity(stream) } }
.flatMapCompletable { entities ->
Completable.fromAction {
runBlocking { database.streamDAO().upsertAll(entities) }
database.streamDAO().upsertAll(entities)
}
}.onErrorComplete()
}