How can I branch out multiple API calls from the result of one API call and collect them after all are finished with Combine?

孤街醉人 提交于 2021-01-29 07:40:47

问题


So, I have this sequence of API calls, where I fetch a employee details, then fetch the company and project details that the employee is associated with. After both fetching are complete, I combine both and publish a fetchCompleted event. I've isolated the relevant code below.

func getUserDetails() -> AnyPublisher<UserDetails, Error>
func getCompanyDetails(user: UserDetails) -> AnyPublisher<CompanyDetails, Error>
func getProjectDetails(user: UserDetails) -> AnyPublisher<ProjectDetails, Error>

If I do this,

func getCompleteUserDetails() -> AnyPublisher<UserFetchState, Never> {

  let cvs = CurrentValueSubject<UserFetchState, Error>(.initial)

  let companyPublisher = getUserDetails()
    .flatMap { getCompanyDetails($0) }
  let projectPublisher = getUserDetails()
    .flatMap { getProjectDetails($0) }
  
  companyPublisher.combineLatest(projectPublisher)
    .sink { cvs.send(.fetchComplete) }
  return cvs.eraseToAnyPublisher()
}

getUserDetails() will get called twice. What I need is fetch the userDetails once and with that, branch the stream into two, map it to fetch the company details and project details and re-combine both. Is there a elegant(flatter) way to do the following.

func getCompleteUserDetails() -> AnyPublisher<UserFetchState, Never> {

  let cvs = CurrentValueSubject<UserFetchState, Error>(.initial)

    getUserDetails()
      .sink {
        let companyPublisher = getCompanyDetails($0)
        let projectPublisher = getProjectDetails($0)
        
        companyPublisher.combineLatest(projectPublisher)
          .sink { cvs.send(.fetchComplete) }
      }
  return cvs.eraseToAnyPublisher()
}

回答1:


You can use the zip operator to get a Publisher which emits a value whenever both of its upstreams emitted a value and hence zip together getCompanyDetails and getProjectDetails.

You also don't need a Subject to signal the fetch being finished, you can just call map on the flatMap.

func getCompleteUserDetails() -> AnyPublisher<UserFetchState, Error> {
    getUserDetails()
        .flatMap { getCompanyDetails(user: $0).zip(getProjectDetails(user: $0)) }
        .map { _ in UserFetchState.fetchComplete }
        .eraseToAnyPublisher()
}

However, you shouldn't need a UserFetchState to signal the state of your pipeline (and especially shouldn't throw away the fetched CompanyDetails and ProjectDetails objects in the middle of your pipeline. You should simply return the fetched CompanyDetails and ProjectDetails as a result of your flatMap.

func getCompleteUserDetails() -> AnyPublisher<(CompanyDetails, ProjectDetails), Error> {
    getUserDetails()
        .flatMap { getCompanyDetails(user: $0).zip(getProjectDetails(user: $0)) }
        .eraseToAnyPublisher()
}



回答2:


The whole idea of Combine is that you construct a pipeline down which data flows. Actually what flows down can be a value or a completion, where a completion could be a failure (error). So:

  • You do not need to make a signal that the pipeline has produced its value; the arrival of that value at the end of the pipeline is that signal.

  • Similarly, you do not need to make a signal that the pipeline's work has completed; a publisher that has produced all the values it is going to produce produces the completion signal automatically, so the arrival of that completion at the end of the pipeline is that signal.

After all, when you receive a letter, the post office doesn't call you up on the phone and say, "You've got mail." Rather, the postman hands you the letter. You don't need to be told you've received a letter; you simply receive it.


Okay, let's demonstrate. The key to understanding your own pipeline is simply to track what kind of value is traveling down it at any given juncture. So let's construct a model pipeline that does the sort of thing you need done. I will posit three types of value:

struct User {
}
struct Project {
}
struct Company {
}

And I will imagine that it is possible to go online and fetch all of that information: the User independently, and the Project and Company based on information contained in the User. I will simulate that by providing utility functions that return publishers for each type of information; in real life these would probably be deferred futures, but I will simply use Just to keep things simple:

func makeUserFetcherPublisher() -> AnyPublisher<User,Error> {
    Just(User()).setFailureType(to: Error.self).eraseToAnyPublisher()
}
func makeProjectFetcherPublisher(user:User) -> AnyPublisher<Project,Error> {
    Just(Project()).setFailureType(to: Error.self).eraseToAnyPublisher()
}
func makeCompanyFetcherPublisher(user:User) -> AnyPublisher<Company,Error> {
    Just(Company()).setFailureType(to: Error.self).eraseToAnyPublisher()
}

Now then, let's construct our pipeline. I take it that our goal is to produce, as the final value in the pipeline, all the information we have collected: the User, the Project, and the Company. So our final output will be a tuple of those three things. (Tuples are important when you are doing Combine stuff. Passing a tuple down the pipeline is extremely common.)

Okay, let's get started. In the beginning there is nothing, so we need an initial publisher to kick off the process. That will be our user fetcher:

let myWonderfulPipeline = self.makeUserFetcherPublisher()

What's coming out the end of that pipeline is a User. We now want to feed that User into the next two publishers, fetching the corresponding Project and Company. The way to insert a publisher into the middle of a pipeline is with flatMap. And remember, our goal is to produce the tuple of all our info. So:

let myWonderfulPipeline = self.makeUserFetcherPublisher()
    // at this point, the value is a User
    .flatMap { (user:User) -> AnyPublisher<(User,Project,Company), Error> in
        // ?
    }
    // at this point, the value is a tuple: (User,Project,Company)

So what goes into flatMap, where the question mark is? Well, we must produce a publisher that produces the tuple we have promised. The tuple-making publisher par excellence is Zip. We have three values in our tuple, so this is a Zip3:

let myWonderfulPipeline = self.makeUserFetcherPublisher()
    .flatMap { (user:User) -> AnyPublisher<(User,Project,Company), Error> in
        // ?
        let result = Publishers.Zip3(/* ? */)
        return result.eraseToAnyPublisher()
    }

So what are we zipping? We must zip publishers. Well, we know two of those publishers — they are the publishers we have already defined!

let myWonderfulPipeline = self.makeUserFetcherPublisher()
    .flatMap { (user:User) -> AnyPublisher<(User,Project,Company), Error> in
        let pub1 = self.makeProjectFetcherPublisher(user: user)
        let pub2 = self.makeCompanyFetcherPublisher(user: user)
        // ?
        let result = Publishers.Zip3(/* ? */, pub1, pub2)
        return result.eraseToAnyPublisher()
    }

We're almost done! What goes in the missing slot? Remember, it must be a publisher. And what's our goal? We want to pass on the very same User that arrived from upstream. And what's the publisher that does that? It's Just! So:

let myWonderfulPipeline = self.makeUserFetcherPublisher()
    .flatMap { (user:User) -> AnyPublisher<(User,Project,Company), Error> in
        let pub1 = self.makeProjectFetcherPublisher(user: user)
        let pub2 = self.makeCompanyFetcherPublisher(user: user)
        let just = Just(user).setFailureType(to:Error.self)
        let result = Publishers.Zip3(just, pub1, pub2)
        return result.eraseToAnyPublisher()
    }

And we're done. No muss no fuss. This is a pipeline that produces a (User,Project,Company) tuple. Whoever subscribes to this pipeline does not need some extra signal; the arrival of the tuple is the signal. And now the subscriber can do something with that info. Let's create the subscriber:

myWonderfulPipeline.sink {
    completion in
    if case .failure(let error) = completion {
        print("error:", error)
    }
} receiveValue: {
    user, project, company in
    print(user, project, company)
}.store(in: &self.storage)

We didn't do anything very interesting — we simply printed the tuple contents. But you see, in real life the subscriber would now do something useful with that data.



来源:https://stackoverflow.com/questions/64714131/how-can-i-branch-out-multiple-api-calls-from-the-result-of-one-api-call-and-coll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!