Swift Combine: Managing Multiple Threads with AnyPublisher and PassthroughSubject

I have some Combine streams that I am merging together to create some state for a feature I’m building. Will spare the details, but the basic gist of this is that the main stream is responsible for accumulating elements from sub-streams with scan(_:). The problematic sub-stream is driven by a PassthroughSubject that’s called from somewhere else in my app. In reality, there’s more happening in the sub-stream that isn’t important, this is just an example.

I’d expect that if I fire events off into the subject from multiple threads, the accumulation in the main stream will be incorrect because of the fact that the Combine can run on whatever thread it wants (and from what I understand, scan(_:) is not inherently thread safe).

If I use receive(on:) in my streams I would expect the accumulation in my main stream to work correctly, but I’m still seeing events being handled out of order.

So far, I’ve tried putting a receive(on:) in my sub stream where I listen to the subject. I’ve also tried adding different attributes to this operator like barrier with no affect. I’ve also tried having putting a lock around the subject which was pointless since subjects handle locking themselves. See below for some code:

struct MainStream {
    enum MainEvent {
        case start
        case setCount(Int)
    }

    struct MainState: Equatable {
        var count: Int
    }

    let queue: DispatchQueue
    let subInteractor: SubStream

    func build(_ upstream: AnyPublisher<MainEvent, Never>) -> AnyPublisher<MainState, Never> {
        let shared = upstream.share().eraseToAnyPublisher()
        let subStream = subInteractor
            .build(
                shared
                    .map { _ in SubStream.SubEvent.observe }
                    .eraseToAnyPublisher()
            )
            .map { state in MainEvent.setCount(state.count) }

        return shared
            .merge(with: subStream)
            .scan(MainState(count: 0)) { accum, event in
                switch event {
                case .start:
                    return accum
                case let .setCount(int):
                    return MainState(count: int)
                }
            }
            .eraseToAnyPublisher()
    }
}

struct SubStream {
    struct SubState {
        let count: Int
    }

    enum SubEvent {
        case observe
    }

    let queue: DispatchQueue
    let subject: PassthroughSubject<Int, Never>

    func build(_ upstream: AnyPublisher<SubEvent, Never>) -> AnyPublisher<SubState, Never> {
        upstream
            .map { event in
                // in production code we respond to the closed over
                // `event`, but not important for this example
                subject
                    .receive(on: queue)
                    .map { int in
                        SubState(count: int)
                    }
            }
            .switchToLatest()
            .receive(on: queue)
            .eraseToAnyPublisher()
    }
}

and here is a unit test that simulates multiple threads:


    var sut: MainStream!

    ... test setup functions

    func test_multipleThreads() {
        let expectation0 = XCTestExpectation(description: #function)

        let expectation1 = XCTestExpectation(description: "expectation1")
        let expectation2 = XCTestExpectation(description: "expectation2")
        let expectation3 = XCTestExpectation(description: "expectation3")

        let queue1 = DispatchQueue(label: "queue1")
        let queue2 = DispatchQueue(label: "queue2")
        let queue3 = DispatchQueue(label: "queue3")

        let expectedResults = [0, 1, 2, 3]
        var receivedOutput: [Int] = []

        sut.build(subject.eraseToAnyPublisher())
            .collect(4)
            .sink { output in
                receivedOutput = output.map { $0.count }
                expectation0.fulfill()
            }
            .store(in: &cancellables)

        subject.send(.setCount(0))
        queue1.async { [self] in
            interactorSubject.send(1)
            expectation1.fulfill()
        }
        queue2.async { [self] in
            interactorSubject.send(2)
            expectation2.fulfill()
        }
        queue3.async { [self] in
            interactorSubject.send(3)
            expectation3.fulfill()
        }

        wait(for: [expectation0, expectation1, expectation2, expectation3], timeout: 0.5)
        XCTAssertEqual(receivedOutput, expectedResults)
    }

The test above rarely passes (~40 times out of 100) so I’m assuming that there’s some threading stuff I am not understanding fully here.

Thanks.

New contributor

Michael Battaglia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật