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
24
votes
1 answer

SwiftUI Binding default value (Argument labels '(wrappedValue:)' do not match any available overloads)

In Swift you can define default values on a struct that can be overwritten on initialization: struct myStruct { var a: Int = 1 } var instance1 = myStruct() // instance1.a -> 1 var instance2 = myStruct(a: 10) // instance2.a -> 10 However when I…
Linus
  • 345
  • 1
  • 2
  • 7
24
votes
5 answers

Is there an alternative to Combine's @Published that signals a value change after it has taken place instead of before?

I would like to use Combine's @Published attribute to respond to changes in a property, but it seems that it signals before the change to the property has taken place, like a willSet observer. The following code: import Combine class A { …
Jon Colverson
  • 2,986
  • 1
  • 24
  • 24
22
votes
1 answer

How do I send an error to a PassthroughSubject?

let myPassthrough = PassthroughSubject() I know I can send a value to a passthrough subject using send: myPassthrough.send("abc") How do I send a failure? I tried: myPassthrough.send(Fail(error:…
Senseful
  • 86,719
  • 67
  • 308
  • 465
22
votes
2 answers

Combine @Published property: get current value during update, from elsewhere

My main problem is that I'm trying to work around the (undocumented) fact that @Published properties don't update the property's value until after subscribers have been notified of the change. I can't seem to get a good way around it. Consider the…
Elliot Schrock
  • 1,406
  • 2
  • 9
  • 20
22
votes
3 answers

SwiftUI How to instantiate PreviewProvider when View requires @Binding in initializer

With SwiftUI (Xcode 11.1), I've got some Views set up with 2-way bindings (using @Binding). Two-way updating works great. However, how can I instantiate the view from the PreviewProvider? For example: struct AddProjectView: View { @Binding…
drewster
  • 5,460
  • 5
  • 40
  • 50
22
votes
8 answers

UserDefaults Binding with Toggle in SwiftUI

I'm trying to figure out the best way to build a simple settings screen bound to UserDefaults. Basically, I have a Toggle and I want: the value a UserDefault to be saved any time this Toggle is changed (the UserDefault should be the source of…
gohnjanotis
  • 6,513
  • 6
  • 37
  • 57
21
votes
2 answers

Binding ViewModel and TextFields with SwiftUI

I'm looking for the best way to create a bind between textfields and ViewModel. At the moment I'm creating a @State for each textfield and I'm manually sending the value from textfield to the viewModel properties when needed. I'm pretty sure this…
MatterGoal
  • 16,038
  • 19
  • 109
  • 186
21
votes
2 answers

Set a given Publishers Failure type to Never in Combine

Is there a way to transform a given AnyPublisher to AnyPublisher?
mike
  • 2,073
  • 4
  • 20
  • 31
20
votes
3 answers

How to update UIViewRepresentable with ObservableObject

I'm trying to learn Combine with SwiftUI and I'm struggling how to update my view (from UIKit) with ObservableObject (previously BindableObject). The issue is that, obviously, method updateUIView will not fire once the @Published object sends the…
Nat
  • 12,032
  • 9
  • 56
  • 103
19
votes
5 answers

SwiftUI and CombineLatest with more than 4 values

I'm starting to experiment with SwiftUI and I have a situation where I want the latest combination of 5 sliders. I had everything working with 4 sliders, using CombineLatest4, then realized I need another slider, but there's no CombineLatest5. Any…
jbm
  • 1,248
  • 10
  • 22
19
votes
4 answers

Optional linking for Swift Combine.framework in Xcode 11

Our application supports iOS 11 and higher. In iOS 13 we use SwiftUI + Combine we wrap import of SwiftUI or Combine framework with correspondent check #if canImport(SwiftUI) or #if canImport(Combine). If we run our app from Xcode 11 under iOS 12 we…
Igor Palaguta
  • 3,579
  • 2
  • 20
  • 32
18
votes
2 answers

Combine framework: how to process each element of array asynchronously before proceeding

I'm having a bit of a mental block using the iOS Combine framework. I'm converting some code from "manual" fetching from a remote API to using Combine. Basically, the API is SQL and REST (in actual fact it's Salesforce, but that's irrelevant to the…
matt
  • 515,959
  • 87
  • 875
  • 1,141
18
votes
4 answers

How to replicate PromiseKit-style chained async flow using Combine + Swift

I was using PromiseKit successfully in a project until Xcode 11 betas broke PK v7. In an effort to reduce external dependencies, I decided to scrap PromiseKit. The best replacement for handling chained async code seemed to be Futures using the new…
Small Talk
  • 747
  • 1
  • 6
  • 15
18
votes
2 answers

ObservedObject inside ObservableObject not refreshing View

I'm trying to display an activity indicator when performing an async request. What I did is creating an ActivityTracker object that will track life cycle of a publisher. This ActivityTracker is an ObservableObject and will be stored in the view…
Yaman
  • 3,949
  • 4
  • 38
  • 60
18
votes
2 answers

Combine: Going from Notification Center addObserver with selector to Notification publisher

I've seen how to transition to Combine using a Publisher from some NotificationCenter code, but have not seen how to do it for something like: NotificationCenter.default.addObserver( self, selector: #selector(notCombine), …
SRMR
  • 3,064
  • 6
  • 31
  • 59