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

Use Observable.defered in start method of coordinator. #1

Open
sergdort opened this issue Aug 4, 2017 · 2 comments
Open

Use Observable.defered in start method of coordinator. #1

sergdort opened this issue Aug 4, 2017 · 2 comments

Comments

@sergdort
Copy link

sergdort commented Aug 4, 2017

Hi, guys!

Great article and idea! Thanks for sharing it ❤️

I have one proposal tho :)

I feel like it would be more aligned with nature of Observable (side effects should happen on subscription) so in the start method e.g LanguageListCoordinator instead of

override func start() -> Observable<CoordinationResult> {
        let viewController = LanguageListViewController.initFromStoryboard(name: "Main")
        let navigationController = UINavigationController(rootViewController: viewController)

        let viewModel = LanguageListViewModel()
        viewController.viewModel = viewModel

        let cancel = viewModel.didCancel.map { _ in CoordinationResult.cancel }
        let language = viewModel.didSelectLanguage.map { CoordinationResult.language($0) }

        rootViewController.present(navigationController, animated: true)

        return Observable.merge(cancel, language)
            .take(1)
            .do(onNext: { [weak self] _ in self?.rootViewController.dismiss(animated: true) })
    }

use Observable.defered

override func start() -> Observable<CoordinationResult> {
        return Observable.deferred {
            let viewController = LanguageListViewController.initFromStoryboard(name: "Main")
            let navigationController = UINavigationController(rootViewController: viewController)

            let viewModel = LanguageListViewModel()
            viewController.viewModel = viewModel

            let cancel = viewModel.didCancel.map { _ in CoordinationResult.cancel }
            let language = viewModel.didSelectLanguage.map { CoordinationResult.language($0) }

            self.rootViewController.present(navigationController, animated: true)

            return Observable.merge(cancel, language)
                .take(1)
                .do(onNext: { [weak self] _ in self?.rootViewController.dismiss(animated: true) })
        }
    }

Doing so we won't trigger navigation in case you do something like

let startObservable = start()
@arthur-here
Copy link
Collaborator

Thanks @sergdort. That's such an awesome suggestion!
I'm going to update the repository and the article as well

@mehdok
Copy link

mehdok commented Jul 10, 2018

@arthur-here did you update the code or article? I used defered and coordinator won't show the view at all :) maybe I did it wrong!

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

3 participants