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

Prevent sink receiveValue closure from being called immediately

Consider the following code (you can c&p it directly into a playground): class Foo: ObservableObject { @Published var bar = "bar" } let foo = Foo() let someSubscriber = foo.$bar .sink { value in print("value is \(value)") …
ff10
  • 3,046
  • 1
  • 32
  • 55
9
votes
4 answers

How to mock DataTaskPublisher?

I'm trying to write some unit tests for my API using URLSession.DataTaskPublisher. I've found an already existing question on Stackoverflow for the same but I'm struggling to implement a working class using the proposed solution. Here's the existing…
G. Marc
  • 4,987
  • 4
  • 32
  • 49
9
votes
4 answers

Add @Published behaviour for computed property

I am trying to make a ObservableObject that has properties that wrap a UserDefaults variable. In order to conform to ObservableObject, I need to wrap the properties with @Published. Unfortunately, I cannot apply that to computed properties, as I use…
Nicolas Degen
  • 1,522
  • 2
  • 15
  • 24
9
votes
2 answers

What’s the alternative for 'Publishers.Once'?

The new Xcode 11 beta 4 has removed Publishers.Once struct from the Combine framework. What is the alternative? Just seems the likely candidate, however, it cannot be used for returning a publisher in methods with return type AnyPublisher
Sudara
  • 4,769
  • 3
  • 34
  • 39
9
votes
2 answers

SwiftUI: animate changes that depend on @ObjectBinding

SwiftUI has implicit animations with .animate(), and explicit ones using .withAnimation(). However, I can't figure out how to animate an image change: struct ImageViewWidget : View { @ObjectBinding var imageLoader: ImageLoader init(imageURL:…
zerohedge
  • 3,185
  • 4
  • 28
  • 63
9
votes
2 answers

IOS 13 Combine Framework - @Published not working ("Unknown attribute 'Published'")

I watched the WWDC 2019 session "Combine in Practice" (https://developer.apple.com/videos/play/wwdc2019/721/). In the video they used the following syntax to create a publisher: @Published var someName: String = "" They did this so that someName…
SwiftedMind
  • 3,701
  • 3
  • 29
  • 63
8
votes
1 answer

What thread does swift Combine `.collect` method emit on?

What is the expected thread on which a .collect operation in Swift's Combine will emit? Specifically I am seeing this code crash in the second precondition, but not the first: return Publishers.MergeMany(publishers) .handleEvents(receiveOutput:…
esilver
  • 27,713
  • 23
  • 122
  • 168
8
votes
2 answers

Combine sink: ignore receiveValue, only completion is needed

Consider the following code: CurrentValueSubject(()) .eraseToAnyPublisher() .sink { completion in switch completion { case .failure(let error): …
Richard Topchii
  • 7,075
  • 8
  • 48
  • 115
8
votes
2 answers

Swift combine retry only for some error types

I have a custom pipeline where I want to have 3 retry attempt for some error codes which are recoverable plus I want to add some short delay for the recoverable error. Anyone has an idea how I can do it? func createRequest(for message: Message) ->…
BilalReffas
  • 8,132
  • 4
  • 50
  • 71
8
votes
3 answers

AnyCancellable.store(in:) with Combine

Let’s say you are using the built-in .store(in:) method on AnyCancellable like so: private var subscriptions = Set() let newPhotos = photos.selectedPhotos newPhotos .map { [unowned self] newImage in return self.images.value +…
Jaythaking
  • 2,200
  • 4
  • 25
  • 67
8
votes
1 answer

SwiftUI/Combine: subscribe to value change of @Binding

I have a view with a view model, and actions in this view can change the view model. To be able to break out logic into reusable pieces, I have part of the view as its own view, with a @Binding to the values it needs to have. Now, I want to be able…
niklassaers
  • 8,480
  • 20
  • 99
  • 146
8
votes
1 answer

How to convert to/from AnyPublisher and AnyPublisher?

I'm wrapping async requests in Combine publishers so they can easily be used across different pipelines. A consumer might hold on to these publishers as follows: struct Dependencies { var loadImageRequest: AnyPublisher var…
Senseful
  • 86,719
  • 67
  • 308
  • 465
8
votes
3 answers

With Combine, how to deallocate the Subscription after a network request

If you use Combine for network requests with URLSession, then you need to save the Subscription (aka, the AnyCancellable) - otherwise it gets immediately deallocated, which cancels the network request. Later, when the network response has been…
Rob N
  • 15,024
  • 17
  • 92
  • 165
8
votes
3 answers

Swift Combine operator with same functionality like `withLatestFrom` in the RxSwift Framework

I'm working on an iOS application adopting the MVVM pattern, using SwiftUI for designing the Views and Swift Combine in order to glue together my Views with their respective ViewModels. In one of my ViewModels I've created a Publisher (type Void)…
Alienbash
  • 554
  • 7
  • 17
8
votes
1 answer

Combine: Convert Closure into Publisher

How to convert: func getResults(completion: ([Result]?, Error) -> Void) Into var resultsPublisher: AnyPublisher<[Result], Error> Just a scheme how I see it is (this syntax doesn't exist): var resultsPublisher: AnyPublisher<[Result], Error> { let…
Paul T.
  • 4,938
  • 7
  • 45
  • 93