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
7
votes
2 answers

Combining custom property wrapper with @Published

I wish to apply a custom property wrapper to a variable already wrapped in @Published, nesting them like (A) @Custom @Published var myVar or (B) @Published @Custom var myVar (notice the application order of the wrappers). In the case of (A) I get…
Milo Wielondek
  • 4,164
  • 3
  • 33
  • 45
7
votes
1 answer

Combine`s subscribe(on:options:) operator

I have a question about the subscribe(on:options:) operator. I would appreciate if anyone can help me to figure it out. So what we have from the documentation: Specifies the scheduler on which to perform subscribe, cancel, and request…
Oleg Kosenko
  • 105
  • 5
7
votes
1 answer

Two-way binding in Swift Combine (UIKit)

I have a UITableViewCell that contain a UISwitch. This cell has its own SwitchCellViewModel. Lets say it contains some Bool value (enabled vs disabled). And ViewController is the one who contains UITableView, creates viewModel for the cell and sets…
Stas Ivanov
  • 917
  • 1
  • 10
  • 22
7
votes
3 answers

What is the reason to store subscription into a subscriptions set?

Combine subscription sample code snippet all store the resulting subscription into the subscriptions set private var subscriptions = Set() Why do we need to do it? future .sink(receiveCompletion: { print($0) }, receiveValue: {…
XY L
  • 25,431
  • 14
  • 84
  • 143
7
votes
1 answer

How to continue subscribing to publisher after error?

I am trying to set up a publisher that will publish a set of integers and at some point may fail. It's slightly contrived but hopefully illustrates principle. Example below. enum NumberError: Int, Error { case isFatal, canContinue } struct…
E_mac
  • 113
  • 7
7
votes
1 answer

Swift Combine how Set works?

I have ViewModel with disposable Set defined this way class ViewModel { private var disposables = Set() func sync() { repo.syncObjects() .handleEvents(receiveCancel: { print("Synced objects:…
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
7
votes
2 answers

SwiftUI stops updates during scrolling of List

Given a List in SwiftUI, once panning begins, updating of views in the list seems to pause until the scrolling has been stopped. Is there a way to prevent this? Consider the following code: class Model: ObservableObject, Identifiable { …
Kevin R
  • 8,230
  • 4
  • 41
  • 46
7
votes
3 answers

View refreshing not triggered when ObservableObject is inherited in SwiftUI

ContentView2 view is not refreshed when model.value changes, if Model conforms to ObservableObject directly instead of inheriting SuperModel then it works fine class SuperModel: ObservableObject { } class Model: SuperModel { @Published var…
Sorin Lica
  • 6,894
  • 10
  • 35
  • 68
7
votes
2 answers

Mapping Swift Combine Future to another Future

I have a method that returns a Future: func getItem(id: String) -> Future { return Future { promise in // alamofire async operation } } I want to use it in another method and covert MediaItem to NSImage, which is a…
Kon
  • 4,023
  • 4
  • 24
  • 38
7
votes
0 answers

How to connect published properties of model and viewmodel in Swift?

Let's assume a model, which implements the protocol ObservableObject and has got a @Published property name. // MARK: Model class ContentSinglePropertyModel: ObservableObject { @Published public var name: String } Now, I would like to display…
Nikolai
  • 659
  • 1
  • 5
  • 10
7
votes
2 answers

How to use Combine framework NSObject.KeyValueObservingPublisher?

I'm trying to use the Combine framework NSObject.KeyValueObservingPublisher. I can see how to produce this publisher by calling publisher(for:options:) on an NSObject. But I'm having two problems: I can include .old in the options, but no .old…
matt
  • 515,959
  • 87
  • 875
  • 1,141
7
votes
2 answers

How do you apply a Combine operator only after the first message has been received?

In Combine, using only the built-in operators, is there a way to skip an operator on the first value but then apply that operator for all subsequent values? Consider the following: publisher .debounce(...) .sink(...) In this arrangement,…
kennyc
  • 5,490
  • 5
  • 34
  • 57
7
votes
2 answers

Combine Publishers.Merge but with Publishers.combineLatest behaviour

I am currently trying to implement the merging of two publishers. But I can't find a solution for my use case. I want to merge 2 publishers that both emit an array of structs of the same type. I want the combined publisher to emit values when…
grahan
  • 2,148
  • 5
  • 29
  • 43
7
votes
2 answers

Binding a SwiftUI Button to AnySubscriber like RxCocoa's button tap

I use the following UIViewController and RxSwift/RxCocoa based piece of code to write a very simply MVVM pattern to bind a UIButton tap event to trigger some Observable work and listen for the result: import UIKit import RxSwift import…
JAHelia
  • 6,934
  • 17
  • 74
  • 134
7
votes
1 answer

Cloudkit with Combine

I am developing an app with SwiftUI and Combine using MVVM architecture targeting only iOS 13. I want to implement Data sync between devices and sharing using CloudKit framework. How can I Combine-ify the CloudKit framework to use in my project? Is…
Mohamed Wasiq
  • 490
  • 4
  • 17