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

后端 未结 3 1650
感情败类
感情败类 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

    Essentially, in your specific situation you're looking at something like this:

    func createIngredients(ingredients: [Ingredient]) -> AnyPublisher<[CreateIngredientMutation.Data], Error> {
        Publishers.MergeMany(ingredients.map(createIngredient(ingredient:)))
            .collect()
            .eraseToAnyPublisher()
    }
    

    This 'collects' all the elements produced by the upstream publishers and – once they have all completed – produces an array with all the results and finally completes itself.

    Bear in mind, if one of the upstream publishers fails – or produces more than one result – the number of elements may not match the number of subscribers, so you may need additional operators to mitigate this depending on your situation.

    The more generic answer, with a way you can test it using the EntwineTest framework:

    import XCTest
    import Combine
    import EntwineTest
    
    final class MyTests: XCTestCase {
        
        func testCreateArrayFromArrayOfPublishers() {
    
            typealias SimplePublisher = Just
    
            // we'll create our 'list of publishers' here. Each publisher emits a single
            // Int and then completes successfully – using the `Just` publisher.
            let publishers: [SimplePublisher] = [
                SimplePublisher(1),
                SimplePublisher(2),
                SimplePublisher(3),
            ]
    
            // we'll turn our array of publishers into a single merged publisher
            let publisherOfPublishers = Publishers.MergeMany(publishers)
    
            // Then we `collect` all the individual publihser elements results into
            // a single array
            let finalPublisher = publisherOfPublishers.collect()
    
            // Let's test what we expect to happen, will happen.
            // We'll create a scheduler to run our test on
            let testScheduler = TestScheduler()
    
            // Then we'll start a test. Our test will subscribe to our publisher
            // at a virtual time of 200, and cancel the subscription at 900
            let testableSubscriber = testScheduler.start { finalPublisher }
    
            // we're expecting that, immediately upon subscription, our results will
            // arrive. This is because we're using `just` type publishers which
            // dispatch their contents as soon as they're subscribed to
            XCTAssertEqual(testableSubscriber.recordedOutput, [
                (200, .subscription),            // we're expecting to subscribe at 200
                (200, .input([1, 2, 3])),        // then receive an array of results immediately
                (200, .completion(.finished)),   // the `collect` operator finishes immediately after completion
            ])
        }
    }
    

提交回复
热议问题