Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

combineLatest doesn't emit any value if the input array is empty #118

Closed
DesmanLead opened this issue Mar 10, 2022 · 5 comments
Closed

combineLatest doesn't emit any value if the input array is empty #118

DesmanLead opened this issue Mar 10, 2022 · 5 comments

Comments

@DesmanLead
Copy link

Example:

func foo(publishers: [AnyPublisher<String, Never>]) {
    publishers
        .combineLatest()
        .sink { strings in print(strings) }
        .store(in: &cancellables)
}

Nothing will be printed if publishers is an empty array.
Feels like the correct behavior is to emit an empty array of String in this case. The caller doesn't need a special handling of an empty input case then.

@freak4pc
Copy link
Member

Hey, I believe this was discussed already here -
This behavior is matched with that of RxSwift and Reactive Streams spec.
An empty publisher doesn't mean an empty value - on the contrary, it means the lack of values.

@DesmanLead
Copy link
Author

Do you have any use cases in mind when this behavior makes sense? Maybe, I'm biased by our use cases.

@freak4pc
Copy link
Member

It's not about a use case it's about guarantees and rules of the operator :)
combineLatest combines publishers once they emit at least a single value - if one of the publishers never emits anything, the entirety of the combination shouldn't. The fact it's an array and not just two or three individual publishers is just an implementation detail, the rules themselves don't change :)

@DesmanLead
Copy link
Author

Sounds reasonable, I'm convinced :)
Do you have any elegant solution in mind for the case when either all the publishers emit at least one value or there are no publishers?
I can't say I'm completely satisfied with if input.isEmpty { return Just([]).eraseToAnyPublisher() } %)
Thanks for sharing your thoughts!

@jasdev
Copy link
Member

jasdev commented Mar 15, 2022

@DesmanLead: .replaceEmpty(with: []) should do the trick. ✅

Even though Ext didn’t decide to go this route, there’s sound theoretical reason for why an empty array should be emitted. I wrote a bit about it here, if you’re curious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants