Search code examples
iosswiftrx-swiftpromisekit

Does PromiseKit have an RxSwift Zip alternative?


I am currently working a project with a requirement to refactor the RxSwift dependencies for introduce PromiseKit.

I am not hugely familiar with RxSwift or PromiseKit so please forgive me if this is an obvious one.

I am looking for a way to handle the Zip use case still

override func start() -> Observable<CoordinationResult> {

    let tabBarController = UITabBarController()
    let tabs: [SectionTab] = [.feed]
    let coordinationResults = Observable.from(configure(tabBarController: tabBarController, withTabs: tabs)).merge()

    window.rootViewController = tabBarController
    window.makeKeyAndVisible()

    return coordinationResults
        .take(1)
}

private func configure(tabBarController: UITabBarController, withTabs tabs: [SectionTab]) -> [Observable<Void>] {
    let navControllers = tabs
        .map { tab -> UINavigationController in
            let navController = BaseNavigationController()
            navController.tabBarItem = UITabBarItem(title: tab.title, image: nil, selectedImage: nil)
            return navController
    }

    tabBarController.viewControllers = navControllers
    tabBarController.view.backgroundColor = UIColor.white // Fix dark shadow in nav bar on segue

    return zip(tabs, navControllers)
        .map { (tab, navController) in
            switch tab {
            case .feed:
                let coordinator = FeedCoordinator(navigationController: navController, dependencies: dependencies)
                return coordinate(to: coordinator)
            }
    }
}

From what I understand, Zip combines 2 Observables and allows you to action on them, emitting a single observable, which is essentially the result of the 2 original observables and your action.


Solution

  • First of all your snippet of code includes Apple's Swift zip which has nothing to do with reactive programming nor RxSwift.
    https://developer.apple.com/documentation/swift/1541125-zip

    In your case it simply combines two arrays tabs and navControllers pair wise.

    Your understanding of reactive Zip operator kinda oversimplifies more complicated cases for zipping streams with more than one emitted value.
    When in doubt always check the marbles diagram: http://reactivex.io/documentation/operators/zip.html

    Hence your initial intuitive understanding matches more CombineLatest http://reactivex.io/documentation/operators/combinelatest.html

    To your original question regardless of the included code - assuming a super simplified scenario in which you Zip or CombineLatest two streams with one emitted value you could achieve something similar in PromiseKit with when(resolved: guarantees).done {