Swift Combine: How to create a single publisher from a list of publishers?

后端 未结 3 1649
感情败类
感情败类 2021-02-04 00:24

Using Apple\'s new Combine framework I want to make multiple requests from each element in a list. Then I want a single result from a reduction of all the the responses. Basical

3条回答
  •  被撕碎了的回忆
    2021-02-04 00:49

    Try something like this if order is important:

    func createIngredients(ingredients: [Ingredient]) -> AnyPublisher<[CreateIngredientMutation.Data], Error> {
        // first attempt
        let results = ingredients
                .map(createIngredient)
        // results = [AnyPublisher]
    
        var resultPublisher = Empty
    
        for result in results {
            resultPublisher = resultPublisher.append(result)
        }
    
        return resultPublisher.collect()
    }
    

提交回复
热议问题