ios – Learn how to Chain Service Requests Serially with Writer and Sharing First Writer

[ad_1]

I’ve three capabilities that should be known as serially. Two of them are publishers. The third one creates sockets. After calling the primary operate(request writer) I have to then flip round and name different requests.

I’m having a tough time with the syntax for chaining and mapping the result of 1 request to the opposite.

My calls so as are


//1
func getCredentialsPublisher(userInfo: String)-> AnyPublisher<CredentialsResponse, Error>
//2
func getSession(token: String) -> AnyPublisher<Session, Error>
//3
func setupSockets(sessionID: String) -> SocketManager?

The getCredentialsPublisher compiles and returns the anticipated info.

The difficulty I’m having is making an attempt to transform getSession writer to return a SocketManager.


func startSession(uniqueId: String) -> AnyPublisher<SocketManager?, By no means>
     return getSession(token: uniqueId)
         .flatMap { response in
             let socket =  self.setupSockets(sessionID: response.id)
             return  Simply(socket)
             .catch { _ in
                 Simply(nil)
              }
          }
         .eraseToAnyPublisher()
}

this provides me a “Can’t convert return expression of sort ‘AnyPublisher<P.Output, Error>’ to return sort ‘AnyPublisher<SocketManager?, By no means>'”

Any concepts why?

since sockets are created as soon as, is it alright to have “By no means” for the failure?

In my viewModel, I exploit it like this


var credentialsSubscriber: AnyCancellable?
var startSessionSubscriber: AnyCancellable?

credentialsSubscriber =.getCredentialsPublisher(userInfo: object)
    .sink { lead to
        print("end result from get credentials: (end result).")
     } receiveValue: { response in
          //A. begin sockets
          startSessionSubscriber = startSession(uniqueId: response.id)
               .sink( receiveValue: { socket in  
                   self.socketManager = socket
           }
          //B. do one thing else that must be achieved when credentials are acquired.
     }

is that this the correct technique to chain calls 1 and a couple of? Do I want completely different subscribers for A. and B.?

[ad_2]

Leave a Reply