skip to Main Content

I want to fire two publishers but second should only fire when first one is complete. I tried it with zip could not achieve it. Following is my code:

    let resetPasscodeConfirmationPublisher = authUseCase.resetPasscodeConfirmation(phoneNumber: phoneNumber, code: pincode, passcode: passcode)
        
    
    let getAuthTokenPublisher = authUseCase.getAccessToken(passcode: passcode)
        
        resetPasscodeConfirmationPublisher.zip(getAuthTokenPublisher).sink { res in
            print(res)
        } receiveValue: { values in
            print(values.0)
            print(values.1)
        }.store(in: &cancellables)

so first, resetPasscodeConfirmationPublisher should run and when it is done and I get some value from it getAuthTokenPublisher should run. Right now I am getting response from getAuthTokenPublisher only and not from the first publisher.

2

Answers


  1. You can use a map operator to do that

    resetPasscodeConfirmationPublisher
        .map { result in
            // do what you need with the result
            return getAuthTokenPublisher
        }
        .switchToLatest()
        .sink(
            receiveCompletion: { completion in
    
            },
            receiveValue: { value in
    
            }
        )
        .store(in: &cancellables)
    
    Login or Signup to reply.
  2. This can help? I don’t sure to understand the question but I think you can start the second publisher on receiveCompletion of first publisher.

    import SwiftUI
    import Combine
    
    var cancellables: Set<AnyCancellable> = []
    
    struct ContentView: View {
        var body: some View {
            VStack {
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundColor(.accentColor)
                Text("Hello, world!")
            }
            .padding()
            .onAppear {
                
                let resetPasscodeConfirmationPublisher = ["p1", "p2", "p3"]
                
                
                let getAuthTokenPublisher = ["a1", "a2", "a3" ]
                
                
                resetPasscodeConfirmationPublisher.publisher
                    .sink(
                        receiveCompletion: { completion in
                            getAuthTokenPublisher.publisher
                                .sink { val in
                                    debugPrint(val)
                                }
                                .store(in: &cancellables)
                        },
                        receiveValue: { value in
                            debugPrint(value)
                        }
                    )
                    .store(in: &cancellables)
            }
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search