Questions tagged [combine]

Combine is Apple's declarative Swift API for processing values over time. It is based on the Reactive Streams semantics. Use this tag for questions about the Combine framework.

Combine is a closed-source Apple framework based on semantics defined by the Reactive Streams initiative.

Combine defines a declarative API in the Swift programming language for processing values over time. Unlike other Swift reactive frameworks such as RxSwift and ReactiveSwift, Combine includes support for back-pressure and flow control at a fundamental level.

Apple first revealed Combine at WWDC on June 3, 2019 and made it available in the first beta release of Xcode 11.

Combine is part of the following SDKs:

  • macOS 10.15 (Catalina) and later,
  • iOS 13 and later,
  • tvOS 13 and later,
  • watchOS 6 and later.
1968 questions
18
votes
2 answers

.send() and .sink() do not seem to work anymore for PassthroughSubject in Xcode 11 Beta 5

In the following code "Test" should be printed in the console when the Button is pressed, but it's not. The event is not send through the publisher. Any idea what happened with PassthroughSubject in Xcode 11 Beta 5 ? (in Xcode 11 Beta 4 it works…
Sorin Lica
  • 6,894
  • 10
  • 35
  • 68
18
votes
1 answer

Combine turn one Publisher into another

I use an OAuth framework which creates authenticated requests asynchronously like so: OAuthSession.current.makeAuthenticatedRequest(request: myURLRequest) { (result: Result) in switch result { case…
jjatie
  • 5,152
  • 5
  • 34
  • 56
17
votes
1 answer

Combine convert Just to AnyPublisher

How to convert Just<[Int]> to AnyPublisher<[Int], Error>. When I use eraseToAnyPublisher() the type is AnyPublisher<[Int], Never> which is not the same as AnyPublisher<[Int], Error> For example I have a simple function which I want to mock temporary…
mkowal87
  • 596
  • 4
  • 19
17
votes
3 answers

How do I properly test a var that changes through a publisher in my viewModel in XCTestCase

Im trying to test a simple publisher within the Combine framework and SwiftUI. My test tests a published bool named isValid in my view model. My view model also has a published username string, that when changes and becomes 3 characters or more…
user1302387
  • 278
  • 2
  • 11
17
votes
2 answers

Swift Combine - Wait for all publishers

I am trying to use Swift combine to run many tasks with the same result. at the moment each task is a publisher that will emit a result. now I am facing a problem that I have to wait for all publishers to emit the element then moving on. kind of…
Ali
  • 287
  • 1
  • 2
  • 13
17
votes
2 answers

Swift Combine: How to specify the Error type of tryMap(_:)?

In the Combine framework, we can throw a generic Error protocol type while using tryMap. However, how can we be more specific about the Error type? For example, let publisher = urlSession.dataTaskPublisher(for: request).tryMap { (data, response) ->…
WildCat
  • 1,961
  • 6
  • 24
  • 35
17
votes
1 answer

How to properly manage a collection of `AnyCancellable`

I'd like all publishers to execute unless explicitly cancelled. I don't mind AnyCancellable going out of scope, however based on docs it automatically calls cancel on deinit which is undesired. I've tried to use a cancellable bag, but AnyCancelable…
pronebird
  • 12,068
  • 5
  • 54
  • 82
17
votes
2 answers

SwiftUI: Generic parameter 'Subject' could not be inferred

I built a LoadingView with SwiftUI for showing some loading stuff in my app while I'm fetching remote data from an API. I am on Xcode Version 11.0 beta 5. This is the LoadingView: struct LoadingView: View where Content: View { @Binding…
Teetz
  • 3,475
  • 3
  • 21
  • 34
17
votes
1 answer

How to set multiple EnvironmentObjects which are same type

I found this question SwiftUI: Putting multiple BindableObjects into Envionment the answer said environmentObject(ObservableObject) returns modified view, therefore I can make call chain for multiple environmentObject. like let rootView =…
WoffOVkee
  • 435
  • 3
  • 16
16
votes
2 answers

Deriving binding from existing SwiftUI @States

I've been playing around with SwiftUI and Combine and feel like there is probably a way to get a hold of the existing @State properties in a view and create a new one. For example, I have a password creation View which holds a password and a…
freebie
  • 2,161
  • 2
  • 19
  • 36
15
votes
3 answers

Swift combine error: method on 'Publisher' requires .Failure' (aka 'WeatherError') and 'Never' be equivalent

I am learning Swift Combine now, found quite easy video tutorial, however for some reason I get error when I try to use my enum in PassthroughSubject() Check this code: import Combine enum WeatherError: Error { case…
Piotr979
  • 199
  • 1
  • 2
  • 10
15
votes
1 answer

How does Swift ReferenceWritableKeyPath work with an Optional property?

Ground of Being: It will help, before reading, to know that you cannot assign a UIImage to an image view outlet's image property through the keypath \UIImageView.image. Here's the property: @IBOutlet weak var iv: UIImageView! Now, will this…
matt
  • 515,959
  • 87
  • 875
  • 1,141
15
votes
2 answers

SwiftUI with NotificationCenter publishers

I want to listen to notifications when the app goes to the background and comes back. I'm trying to use the NotificationCenter publishers and have the SwiftUI view listen to them. I can use a few methods to do it and I'm trying to use two of them…
hastoro11
  • 203
  • 1
  • 2
  • 5
15
votes
3 answers

Make custom Publisher run on a different DispatchQueue on Swift Combine

I created a function that returns a custom Publisher in Swift Combine using the code below: func customPubliher() -> AnyPublisher { return Future { promise in promise(.success(true)) …
vegidio
  • 822
  • 2
  • 11
  • 32
15
votes
4 answers

How do I pass data from a child view to a parent view to another child view in SwiftUI?

as the title suggests, I'm trying to pass data from one child view (A), to another child view (B) through the parent view (P). The parent view looks like this: @State var rectFrame: CGRect = .zero var body: some View { childViewA(rectFrame:…
cyril
  • 3,020
  • 6
  • 36
  • 61