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
32
votes
5 answers

Difference between CurrentValueSubject and @Published

So I'm digging into combine and this question came up. Is there any real difference between using CurrentValueSubject (and setting its value using currentValueSubject.value) or using a @Published var and accessing its publisher with a $? I mean I…
32
votes
8 answers

Combine: how to replace/catch an error without completing the original publisher?

Given the following code: enum MyError: Error { case someError } myButton.publisher(for: .touchUpInside).tryMap({ _ in if Bool.random() { throw MyError.someError } else { return "we're in…
swalkner
  • 16,679
  • 31
  • 123
  • 210
30
votes
5 answers

Best data-binding practice in Combine + SwiftUI?

In RxSwift it's pretty easy to bind a Driver or an Observable in a View Model to some observer in a ViewController (i.e. a UILabel). I usually prefer to build a pipeline, with observables created from other observables, instead of "imperatively"…
Enrico Querci
  • 543
  • 2
  • 6
  • 11
30
votes
4 answers

SwiftUI and MVVM - Communication between model and view model

I've been experimenting with the MVVM model that's used in SwiftUI and there are some things I don't quite get yet. SwiftUI uses @ObservableObject/@ObservedObject to detect changes in a view model that trigger a recalculation of the body property to…
SwiftedMind
  • 3,701
  • 3
  • 29
  • 63
29
votes
4 answers

Can a Swift Property Wrapper reference the owner of the property its wrapping?

From within a property wrapper in Swift, can you someone refer back to the instance of the class or struck that owns the property being wrapped? Using self doesn't obviously work, nor does super. I tried to pass in self to the property wrapper's…
kennyc
  • 5,490
  • 5
  • 34
  • 57
28
votes
4 answers

Is there a way to avoid using AnyPublisher/eraseToAnyPublisher all over the place?

I'm just learning how to use Combine. I have experience with Rx (RxSwift and RxJava) and I'm noticing that it's quite similar. However, one thing that is quite different (and kind of annoying) is that the Publisher protocol doesn't use generics for…
jchitel
  • 2,999
  • 4
  • 36
  • 49
28
votes
1 answer

Error: Initializer 'init(_:)' requires that 'Binding' conform to 'StringProtocol'

I am getting the above error and couldn't figure out how to solve it. I have an array of objects that contain a boolean value, and need to show a toggle for each of these boolean. Below is the code. class Item: Identifiable { var id: String …
user1366265
  • 1,306
  • 1
  • 17
  • 28
28
votes
9 answers

How to define a protocol to include a property with @Published property wrapper

When using @Published property wrapper following current SwiftUI syntax, it seems very hard to define a protocol that includes a property with @Published, or I definitely need help :) As I'm implementing dependency injection between a View and it's…
UndergroundFox
  • 1,011
  • 2
  • 12
  • 11
27
votes
4 answers

SwiftUI Combine Debounce TextField

I have a SwiftUI app with SwiftUI App life cycle. I'm trying to setup a standard way to add typing debounce to TextFields. Ideally, I'd like to create my own TextField modifier that can easily be applied to views that have many textfields to edit.…
JohnSF
  • 3,736
  • 3
  • 36
  • 72
27
votes
5 answers

SwiftUI - make sure to publish values from the main thread (via operators like receive(on:)) on model updates

My app contains a resource heavy operation that populates an Array based on data pulled from an XML feed. I do not want this operation to lock up the main thread (and the UI when the array is given new data), so it's done in the background. let…
d5automations
  • 419
  • 1
  • 6
  • 13
27
votes
4 answers

How can I get data from ObservedObject with onReceive in SwiftUI?

In my SwiftUI app, I need to get data from ObservedObject each time the value change. I understood that we could do that with .onReceive? I don't understand well the documentation of Apple about it. I don't know how I can do this. My code: import…
Guillaume
  • 1,500
  • 3
  • 24
  • 33
27
votes
6 answers

How to prevent strong reference cycles when using Apple's new Combine framework (.assign is causing problems)

I don't quite understand how to properly store subscribers inside a class so that they persist but don't prevent the object from being deinitialized. Here's an example where the object won't deinit: import UIKit import Combine class Test { …
SwiftedMind
  • 3,701
  • 3
  • 29
  • 63
27
votes
3 answers

didSet for a @Binding var in Swift

Normally we can use didSet in swift to monitor the updates of a variable. But it didn't work for a @Binding variable. For example, I have the following code: @Binding var text { didSet { ...... } } But the didSet is never been…
Bagusflyer
  • 12,675
  • 21
  • 96
  • 179
27
votes
5 answers

Create a Timer Publisher using Swift Combine

I've been watching the Data Flow Through SwiftUI WWDC talk. They have a slide with a sample code where they use a Timer publisher that gets connected to a SwiftUI View, and updates the UI with the time. I'm working on some code where I want to do…
eivindml
  • 2,197
  • 7
  • 36
  • 68
26
votes
2 answers

SwiftUI how to prevent view to reload whole body

Basically I try to figure out when my viewModel get updated, it will notify view and it will refresh whole body. How to avoid that. For example if my view GoLiveView already present another view BroadcasterView, and later my goLiveViewModel get…
Vkukjans
  • 363
  • 1
  • 3
  • 8