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

Publisher vs AnyPublisher in Combine

What is the role of AnyPublisher in Combine, and why in many examples, including inWWDC Combine In practice, 27:40 they return AnyPublisher, using .eraseToAnyPublisher, and not just return a Publisher? The Apple Documents says Use AnyPublisher to…
Hamoonist
  • 2,286
  • 3
  • 19
  • 36
13
votes
4 answers

Published works for single object but not for array of objects

I am trying to make individually moveable objects. I am able to successfully do it for one object but once I place it into an array, the objects are not able to move anymore. Model: class SocialStore: ObservableObject { @Published var socials :…
sfung3
  • 2,227
  • 1
  • 9
  • 30
12
votes
2 answers

SwiftUI: How can I catch changing value from observed object when I execute function

I have a problem with observed object in SwiftUI. I can see changing values of observed object on the View struct. However in class or function, even if I change text value of TextField(which is observable object) but "self.codeTwo.text still did…
alice_ix
  • 199
  • 1
  • 2
  • 8
12
votes
4 answers

Swift Combine - How to get a Publisher that delivers events for every character change of UITextField's text property

I noticed that textField.publisher(for: \.text) delivers events when editing finishes, but not for every character/editing change. How do I get a Publisher, that sends evens for every change? In ReactiveSwift it would…
thetrutz
  • 1,395
  • 13
  • 12
12
votes
6 answers

How to observe a TextField value with SwiftUI and Combine?

I'm trying to execute an action every time a textField's value is changed. @Published var value: String = "" var body: some View { $value.sink { (val) in print(val) } return TextField($value) } But I get…
Sorin Lica
  • 6,894
  • 10
  • 35
  • 68
11
votes
3 answers

Combine onChange and onAppear events in SwiftUI view?

I'm observe a property on a view using the onChange modifier. However, I'd also like the same piece of code to run on the initial value as well because sometimes the data is injected in the initializer or asynchronously loaded later. For example, I…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
11
votes
3 answers

Combine previous value using Combine

How can I rewrite ReactiveSwift/ReactiveCocoa code using Combine framework? I attached screenshot what combinePrevious mean from docs. let producer = SignalProducer([1, 2, 3]).combinePrevious(0) producer.startWithValues { value in …
Taras
  • 1,485
  • 1
  • 16
  • 31
11
votes
3 answers

Combine: publish elements of a sequence with some delay

I'm new to Combine and I'd like to get a seemingly simple thing. Let's say I have a collection of integer, such as: let myCollection = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] I'd like to publish each element with a delay of, for example, 0.5 seconds. print…
superpuccio
  • 11,674
  • 8
  • 65
  • 93
11
votes
2 answers

async operations using Combine and SwiftUI

I'm trying to figure out how to work with async operations using Combine and SwiftUI. For example, I have a HealthKitManager class that, among other things, handles requesting health store authorisation… final class HealthKitManager { enum…
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
11
votes
9 answers

How to zip more than 4 publishers

I'm using Swift Combine for my API requests. Now I'm facing a situation where I want to have more than 4 parallel requests that I want to zip together. Before I had exactly 4 requests that I zipped together using Zip4() operator. I can imagine that…
G. Marc
  • 4,987
  • 4
  • 32
  • 49
11
votes
3 answers

URLSession.shared.dataTaskPublisher not working on IOS 13.3

When trying to make a network request, I'm getting an error finished with error [-999] Error Domain=NSURLErrorDomain Code=-999 "cancelled" If I use URLSession.shared.dataTask instead of URLSession.shared.dataTaskPublisher it will work on IOS 13.3.…
Rasul
  • 727
  • 7
  • 20
11
votes
1 answer

Apple Combine framework: How to execute multiple Publishers in parallel and wait for all of them to finish?

I am discovering Combine. I wrote methods that make HTTP requests in a "combine" way, for example: func testRawDataTaskPublisher(for url: URL) -> AnyPublisher { var request = URLRequest(url: url, …
kampro
  • 705
  • 1
  • 7
  • 20
11
votes
2 answers

Swift Combine chaining .mapError()

I'm trying to achieve something similar to scenario presented below (create URL, request to server, decode json, error on every step wrapped in custom NetworkError enum): enum NetworkError: Error { case badUrl case noData case…
Michcio
  • 2,708
  • 19
  • 26
11
votes
1 answer

SwiftUI @Binding doesn't refresh View

I have a simple master/detail interface where the detail view modifies an item in an array. Using the below, the model is updated properly, but SwiftUI doesn't refresh the View to reflect the change. Model: struct ProduceItem: Identifiable { let…
smr
  • 890
  • 7
  • 25
10
votes
1 answer

Swift Combine: Could not extract a String from KeyPath Swift.KeyPath

I'm trying to observer any changes in object in the core data instance. Here is my code: class MyClass { @objc dynamic var str: String? } final class Article: NSObject { @objc dynamic var title: String? @objc dynamic var summary:…
user2924482
  • 8,380
  • 23
  • 89
  • 173