Hello, just started using Reaktive and have a gene...
# reaktive
a
Hello, just started using Reaktive and have a general question of when to use
subscribeOn
and
observeOn
. When I was looking at the Todo sample I saw the repository had the following which if I understand correctly will make sure the values being observed are consumed on the background thread? Is there a reason to not also chain a
subscribeOn(ioScheduler)
to enforce all values computed are on a background thread? Or is that just SqlDelight only doing queries on a background thread that prevents the need for that?
Copy code
private fun <T : Any> query(query: (TodoDatabaseQueries) -> Query<T>): Single<Query<T>> =
        queries
            .observeOn(ioScheduler)
            .map(query)
I noticed that in one of the stores that
observeOn(mainScheduler)
is only called to make sure updates to UI are done on the UI, but there is no
subscribeOn(ioScheduler)
so was curious how the computations were being done on a background thread?
Copy code
database
                .updates
                .observeOn(mainScheduler)
                .map(Result::ItemsLoaded)
                .subscribeScoped(onNext = ::dispatch)
Thanks!
a
Reaktive follows Reactive Extensions style, and is inspired by Rxjava. I recommend to read the readme first, especially Kotlin/Native pitfalls. Re subscribeOn: I usually do subscribeOn exactly at the place where thread switching is required. I believe it should be implementation details of a worker function, not its subscribers. In case of the TodoApp sample, it uses SQLDelight, which provides usual synchronous API. Retrieving a Query is a non-blocking operation, but executing a Query is blocking. The thread switching is done here: https://github.com/JetBrains/compose-jb/blob/5dc4f1d9d9d19505836cbe0250f1b7752aae6851/examples/todoapp/common/database/src/commonMain/kotlin/example/todo/common/database/DefaultTodoSharedDatabase.kt#L64 It takes a Query synchronously on whatever thread and then emits the Query on IO scheduler. The Query is then executed somewhere in the downstream.
a
Oh I see now, thank you for the explanation. I will go check out the documentation as well!
K 1