Rework thread managment

This commit is contained in:
tzugen 2023-07-24 20:33:19 +02:00
parent eab55a7837
commit 351f0018db
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
2 changed files with 24 additions and 31 deletions

View File

@ -181,10 +181,9 @@ class MediaPlayerManager(
createMediaController(onCreated) createMediaController(onCreated)
rxBusSubscription += rxBusSubscription += RxBus.activeServerChangingObservable
RxBus // All interaction with the Media3 needs to happen on the main thread
.activeServerChangingObservable .subscribeOn(RxBus.mainThread())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe { oldServer -> .subscribe { oldServer ->
if (oldServer != OFFLINE_DB_ID) { if (oldServer != OFFLINE_DB_ID) {
// When the server changes, the playlist can retain the downloaded songs. // When the server changes, the playlist can retain the downloaded songs.
@ -208,19 +207,19 @@ class MediaPlayerManager(
isJukeboxEnabled = jukebox isJukeboxEnabled = jukebox
} }
rxBusSubscription += RxBus.throttledPlaylistObservable.subscribe { rxBusSubscription += RxBus.throttledPlaylistObservable
// Even though Rx should launch on the main thread it doesn't always :( // All interaction with the Media3 needs to happen on the main thread
mainScope.launch { .subscribeOn(RxBus.mainThread())
.subscribe {
serializeCurrentSession() serializeCurrentSession()
} }
}
rxBusSubscription += RxBus.throttledPlayerStateObservable.subscribe { rxBusSubscription += RxBus.throttledPlayerStateObservable
// Even though Rx should launch on the main thread it doesn't always :( // All interaction with the Media3 needs to happen on the main thread
mainScope.launch { .subscribeOn(RxBus.mainThread())
.subscribe {
serializeCurrentSession() serializeCurrentSession()
} }
}
rxBusSubscription += RxBus.shutdownCommandObservable.subscribe { rxBusSubscription += RxBus.shutdownCommandObservable.subscribe {
clear(false) clear(false)

View File

@ -1,6 +1,5 @@
package org.moire.ultrasonic.service package org.moire.ultrasonic.service
import android.os.Looper
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.CompositeDisposable import io.reactivex.rxjava3.disposables.CompositeDisposable
@ -12,14 +11,9 @@ import org.moire.ultrasonic.domain.Track
class RxBus { class RxBus {
/*
* TODO: mainThread() seems to be not equal to the "normal" main Thread, so it causes
* a lot of often unnecessary thread switching. It looks like observeOn can actually
* be removed in many cases
*/
companion object { companion object {
private fun mainThread() = AndroidSchedulers.from(Looper.getMainLooper()) fun mainThread() = AndroidSchedulers.mainThread()
val shufflePlayPublisher: PublishSubject<Boolean> = val shufflePlayPublisher: PublishSubject<Boolean> =
PublishSubject.create() PublishSubject.create()