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

No ability to inject own scheduler #51

Open
ivanmkc opened this issue Feb 27, 2018 · 6 comments
Open

No ability to inject own scheduler #51

ivanmkc opened this issue Feb 27, 2018 · 6 comments

Comments

@ivanmkc
Copy link
Collaborator

ivanmkc commented Feb 27, 2018

I'm using RxTest to test my network services which makes use of a virtual time TestScheduler. However, in Single+ModelMapper, you have fixed schedulers which are incompatible with RxTest as they would have to be replaced by the TestScheduler for unit tests. Similar discussion is here: ReactiveX/RxSwift#1100

Fix would be to do dependency injection of schedulers, perhaps with a default value.

Problematic code here:

public func map<T: Mappable>(to type: T.Type, keyPath: String? = nil) -> Single<T> {
        return observeOn(ConcurrentDispatchQueueScheduler(qos: .background))
            .flatMap { response -> Single<T> in
                return Single.just(try response.map(to: type, keyPath: keyPath))
            }
            .observeOn(MainScheduler.instance)
    }
@sunshinejr
Copy link
Owner

Hi @ivanmkc. You are right, it is a bug on my end. I removed the observeOn lines from Observable extension, but forgot to do it for Single. Would you be up for removing these in a pull request? I'd be more than happy to review it.

@ivanmkc
Copy link
Collaborator Author

ivanmkc commented Feb 27, 2018

Hi, I went with a solution in my fork that injected the scheduler into the function. However, it breaks existing code and I'm not sure what RxSwift guidelines suggest to do in this case. Are you saying that developers using your library should handle the schedulers themselves when they call the map function (as in the alternative solution below)?

My current fork's solution:

public func map<T: Mappable>(to type: T.Type, scheduler: SchedulerType, keyPath: String? = nil) -> Single<T> {
        return self.observeOn(scheduler)
            .flatMap { response -> Single<T> in
                return Single.just(try response.map(to: type, keyPath: keyPath))
        }
    }

Alternative solution:

public func map<T: Mappable>(to type: T.Type, scheduler: SchedulerType, keyPath: String? = nil) -> Single<T> {
        return flatMap { response -> Single<T> in
                return Single.just(try response.map(to: type, keyPath: keyPath))
        }
    }

and at the call site:

return provider
            .rx
            .request(targetType)
            .observeOn(mappingScheduler)
            .map(to: T.self)
            .asObservable()

@sunshinejr
Copy link
Owner

@ivanmkc Yeah, I think that developers should handle switching between schedulers themselves (your alternative solution).

@ivanmkc
Copy link
Collaborator Author

ivanmkc commented Feb 27, 2018

Ok, I tried to search for some RxSwift guideline on where it states this what couldn't find anything. I will submit a PR with the alternative.

Thanks!

@ivanmkc
Copy link
Collaborator Author

ivanmkc commented Feb 27, 2018

Submitted a PR.

@sunshinejr
Copy link
Owner

@ivanmkc there is no strict rule for managing schedulers in external libraries (as far as I know), so this is probably left out to developers.

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