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
110
votes
15 answers

How to tell SwiftUI views to bind to nested ObservableObjects

I have a SwiftUI view that takes in an EnvironmentObject called appModel. It then reads the value appModel.submodel.count in its body method. I expect this to bind my view to the property count on submodel so that it re-renders when the property…
rjkaplan
  • 3,138
  • 5
  • 27
  • 33
96
votes
3 answers

SwiftUI: Random "Extra argument in call" error

So I'm trying to learn SwiftUI and Combine. I usually start new tech by making a simple tip calculator. I seem to be getting a random "Extra argument in call." error while coding Here is my SwiftUI File import SwiftUI internal enum ReceiptRowType…
Kwoner
  • 963
  • 1
  • 6
  • 7
88
votes
6 answers

An equivalent to computed properties using @Published in Swift Combine?

In imperative Swift, it is common to use computed properties to provide convenient access to data without duplicating state. Let's say I have this class made for imperative MVC use: class ImperativeUserManager { private(set) var currentUser:…
rberggreen
  • 983
  • 1
  • 6
  • 6
67
votes
6 answers

How to update @FetchRequest, when a related Entity changes in SwiftUI?

In a SwiftUI View i have a List based on @FetchRequest showing data of a Primary entity and the via relationship connected Secondary entity. The View and its List is updated correctly, when I add a new Primary entity with a new related secondary…
Björn B.
  • 753
  • 1
  • 7
  • 10
66
votes
2 answers

What does the dollar sign do in Swift / SwiftUI?

This tutorial by Apple about SwiftUI uses a dollar sign to bind data, and I‘m having trouble finding more information about this data binding in SwiftUI. Toggle(isOn: $showFavoritesOnly) { You use the $ prefix to access a binding to a state…
vrwim
  • 13,020
  • 13
  • 63
  • 118
60
votes
5 answers

Cannot convert value of type 'Published.Publisher' to expected argument type 'Binding'

When trying to compile the following code: class LoginViewModel: ObservableObject, Identifiable { @Published var mailAdress: String = "" @Published var password: String = "" @Published var showRegister = false @Published var…
Jan Koch
  • 603
  • 1
  • 5
  • 4
56
votes
5 answers

iOS Swift Combine: cancel a Set

If I have stored a cancellable set into a ViewController: private var bag = Set() Which contains multiple subscription. 1 - Should I cancel subscription in deinit? or it does the job automatically? 2 - If so, how can I cancel all…
Andrea Miotto
  • 7,084
  • 8
  • 45
  • 70
53
votes
6 answers

What is PassthroughSubject & CurrentValueSubject

I happen to look into Apple's new Combine framework, where I see two things PassthroughSubject CurrentValueSubject Can someone explain to me what is meaning & use of them?
Nasir
  • 1,617
  • 2
  • 19
  • 34
53
votes
5 answers

RunLoop vs DispatchQueue as Scheduler

When using new Combine framework you can specify the scheduler on which to receive elements from the publisher. Is there a big difference between RunLoop.main and DispatchQueue.main in this case when assigning publisher to UI element? The first one…
mikro098
  • 2,173
  • 2
  • 32
  • 48
45
votes
4 answers

Binding value from an ObservableObject

Aim: I have a model which is an ObservableObject. It has a Bool property, I would like to use this Bool property to initialise a @Binding variable. Questions: How to convert an @ObservableObject to a @Binding ? Is creating a @State the only way to…
user1046037
  • 16,755
  • 12
  • 92
  • 138
44
votes
4 answers

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

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. Basically I want to go from list of publishers to a single publisher that holds…
Andre Carrera
  • 2,606
  • 2
  • 12
  • 15
37
votes
3 answers

Using Just with flatMap produce Failure mismatch. Combine

I have such code func request(request: URLRequest) -> AnyPublisher { return Just(request) .flatMap { request in RequestManager.request(request) // returns AnyPublisher } …
Tikhonov Aleksandr
  • 13,945
  • 6
  • 39
  • 53
34
votes
2 answers

Accessing ViewModel field in SwiftUI using Xcode 12: "Accessing State's value outside of being installed on a View"

I think this error message is new to SwiftUI in Xcode 12 since it gave 0 hits in Google while the message itself is fairly generic: Accessing State's value outside of being installed on a View. This will result in a constant Binding of the initial…
Lucas van Dongen
  • 9,328
  • 7
  • 39
  • 60
34
votes
1 answer

How can I unwrap an optional value inside a binding in Swift?

I'm building an app using SwiftUI and would like a way to convert a Binding to a Binding. In my app I have an AvatarView which knows how to render an image for a particular user. struct AvatarView: View { @Binding var userData:…
rjkaplan
  • 3,138
  • 5
  • 27
  • 33
33
votes
9 answers

Combine framework serialize async operations

How do I get the asynchronous pipelines that constitute the Combine framework to line up synchronously (serially)? Suppose I have 50 URLs from which I want to download the corresponding resources, and let's say I want to do it one at a time. I know…
matt
  • 515,959
  • 87
  • 875
  • 1,141
1
2 3
99 100