Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.do(onSubscribed:) and .do(onSubscribe:) don't respect .observe(on:) #2636

Open
c128128 opened this issue Oct 16, 2024 · 5 comments
Open

.do(onSubscribed:) and .do(onSubscribe:) don't respect .observe(on:) #2636

c128128 opened this issue Oct 16, 2024 · 5 comments

Comments

@c128128
Copy link

c128128 commented Oct 16, 2024

Short description of the issue:

1 Case

Looks like .do(onSubscribed:) and .do(onSubscribe:) don't respect .observe(on:).

example:

  Observable<String>.just("1")
      .observe(on: SerialDispatchQueueScheduler(internalSerialQueueName: "observe"))
      .do(onSubscribed: {
          print("do, isMain ==> \(Thread.isMainThread), DispatchQueue ==> \(DispatchQueue.name())")
      })
      .subscribe(onNext: { _ in
          print("subscribe, isMain ==> \(Thread.isMainThread), DispatchQueue ==> \(DispatchQueue.name())")
      })

result:

do, isMain ==> true, DispatchQueue ==> main-thread
subscribe, isMain ==> false, DispatchQueue ==> observe

expected:

do, isMain ==> false, DispatchQueue ==> observe
subscribe, isMain ==> false, DispatchQueue ==> observe

2 Case

Also might be related:

Looks like .do(onSubscribed:) and .do(onSubscribe:) don't respect .subscribe(on:) if it is above do statement.

example:

Observable<String>.just("1")
    .subscribe(on: SerialDispatchQueueScheduler(internalSerialQueueName: "subscribe"))
    .do(onSubscribed: {
        print("do, isMain ==> \(Thread.isMainThread), DispatchQueue ==> \(DispatchQueue.name())")
    })
    .subscribe(onNext: { _ in
        print("subscribe, isMain ==> \(Thread.isMainThread), DispatchQueue ==> \(DispatchQueue.name())")
    })

result:

subscribe, isMain ==> false, DispatchQueue ==> subscribe
do, isMain ==> true, DispatchQueue ==> main-thread

expected:

subscribe, isMain ==> false, DispatchQueue ==> subscribe
do, isMain ==> false, DispatchQueue ==> subscribe
@danielt1263
Copy link
Collaborator

In both cases you are subscribing on the main thread. It seems to me that the result you are seeing is correct. Note that if you put the do(onSubscribed:) before the subscribe(on:) then you will see isMain equal to false. Because everything in the stream above the subscribe(on:) is subscribed on the Scheduler provided.

@c128128
Copy link
Author

c128128 commented Oct 16, 2024

I was expecting all operators to behave the same. For example, if I use .map() instead of .do(), it would behave as expected (do, isMain ==> false, DispatchQueue ==> observe).

@danielt1263
Copy link
Collaborator

danielt1263 commented Oct 16, 2024

The do(onNext:) (which is the closest to the map) does receive on the same thread as the subscribe(onNext:)... Subscription is not the same as event emission so they won't necessarily happen on the same thread.

@c128128
Copy link
Author

c128128 commented Oct 16, 2024

Got it. Then https://github.com/ReactiveX/RxSwift/blob/main/Documentation/Schedulers.md is wrong.

From Schedulers.md:

If you want to perform work on a different scheduler just use observeOn(scheduler) operator.

@danielt1263
Copy link
Collaborator

danielt1263 commented Oct 16, 2024

What are you talking about? The example is exactly correct. It seems like you are confused as to the difference between subscription and event propagation. Those are different processes.

Use do(onNext:) instead of do(onSubscribed:)...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants