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

Communication between ViewModels with SwiftUI and Combine (ObservableObject vs Binding)

This is a general question about SwiftUI and the architecture, so I'll take a simple but problematic example. Initial project : I have a first View which displays a list of Items. This list is managed by a class (which I called ListViewModel here).…
Adrien
  • 1,579
  • 6
  • 25
10
votes
4 answers

Swift Combine sink stops receiving values after first error

Im moving my project to Combine from RxSwift I have a logic where I want publisher to emit event every time I click button. Acrually clicking button executed pushMe.send() pushMe .print("Debug") .flatMap { (res) ->…
Sergey Brazhnik
  • 661
  • 4
  • 13
10
votes
2 answers

@State vs @ObservableObject - which and when?

I'm currently getting familiar with SwiftUI and Combine frameworks. And I'm not really getting the difference between these two approaches. When we have to keep track of some data (say, a list of tasks), we can declare a @State variable, and it's…
Alex.K
  • 234
  • 4
  • 12
10
votes
2 answers

Combine: can't use `.assign` with structs - why?

I'm seeing some struct vs class behavior that I don't really don't understand, when trying to assign a value using Combine. Code: import Foundation import Combine struct Passengers { var women = 0 var men = 0 } class Controller { @Published…
Kevin Renskers
  • 5,156
  • 4
  • 47
  • 95
10
votes
2 answers

Swift Combine properties inheritance throws 'Fatal error: Call of deleted method' on Xcode 11.4 beta 2 / iOS 13.4

I'm trying to using Swift Combine to get the changed event of a property. I have this class that publish the isLogged property class CurrentUser: Account { static let me = CurrentUser() //Singleton @Published var isLogged: Bool =…
Oscar TJ
  • 524
  • 3
  • 16
10
votes
2 answers

Understanding share() in Combine

As we know, usually publishers are struct. What will change if it is a class? Let's consider we have 1 publisher which emits 1 value and 2 subscribers, which subscribes to it. let p1 = Just(20) let s1 = p1.print().sink { _ in } let s2 =…
Tikhonov Aleksandr
  • 13,945
  • 6
  • 39
  • 53
10
votes
2 answers

Swift. Combine. Is there any way to call a publisher block more than once when retry?

I want to make a network request more than one time when some error occurs using retry() from Swift/Combine. The block inside the publisher is called once only which means one only one request is made for a real app when error happens. My code…
Dmitry
  • 171
  • 3
  • 12
10
votes
4 answers

How to detect a value change of a Datepicker using SwiftUI and Combine?

How would you detect a change of value of a Datepicker while using SwiftUI and Combine? I need to invoke a method whenever the datepicker wheel is moved, to update a Text and a Slider. I have looked for specific methods to identify the value change…
Salva
  • 707
  • 2
  • 9
  • 18
9
votes
1 answer

Swift Combine how to skip an Event

I use a form for a logging page in my app and I have a bind on the footer to display any error, as you can see below : ContentView.Swift : Form { Section(footer: Text(self.viewModel.errorMessage)) ViewModel.swift init() { …
mart-thomas
  • 154
  • 1
  • 7
9
votes
3 answers

How to trigger Timer.publish() right away?

I created a timer via combine which emits Date and ignores errors using this code: let timer: AnyPublisher = Timer.publish(every: 5, on: .main, in: RunLoop.Mode.common) .autoconnect() .map { _ in Date() } .replaceError(with:…
Senseful
  • 86,719
  • 67
  • 308
  • 465
9
votes
1 answer

Swift Combine publishers vs completion handler and when to cancel

I know in general a publisher is more powerful than a closure, however I want to ask and discuss a specific example: func getNotificationSettingsPublisher() -> AnyPublisher { let notificationSettingsFuture =…
Janosch Hübner
  • 1,584
  • 25
  • 44
9
votes
2 answers

How do I check the current progress of URLSession.dataTaskPublisher?

I'm using a dataTaskPublisher to fetch some data: func downloadData(_ req: URLRequest) { self.cancelToken = dataTaskPublisher(for: req).sink { /* ... */ } } If the function is called while the request is in progress, I would like to…
chustar
  • 12,225
  • 24
  • 81
  • 119
9
votes
3 answers

Swift Combine - delaying a publisher

TL;DR I want to delay a publication, but can't figure out how to, er, combine the parts In Brief I have a Publisher let generator = PassthroughSubject() and want somehow to use the modifier .delay(for: 2, scheduler: RunLoop.main) so…
Andrew Duncan
  • 3,553
  • 4
  • 28
  • 55
9
votes
2 answers

Remove from array of AnyCancellable when publisher finishes

Is there a good way to handle an array of AnyCancellable to remove a stored AnyCancellable when it's finished/cancelled? Say I have this import Combine import Foundation class Foo { private var cancellables = [AnyCancellable]() func…
Rico Crescenzio
  • 3,952
  • 1
  • 14
  • 28
9
votes
1 answer

Swift Combine sink value is different from value of @Published property

I'm running into an issue where in Combine where I have a boolean @Published property. When I set it to true, the sink closure is run and I can look at the value being received. It's true. But when I compare it against the actual property that I am…
YichenBman
  • 5,011
  • 8
  • 46
  • 65