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
15
votes
4 answers

Convert a @State into a Publisher

I want to use a @State variable both for the UI and for computing a value. For example, let's say I have a TextField bound to @State var userInputURL: String = "https://". How would I take that userInputURL and connect it to a publisher so I can map…
Ryan
  • 6,432
  • 7
  • 40
  • 54
15
votes
1 answer

Crash on Canvas SwiftUI

i'm implementing a little app with new iOS framework SwiftUI. I'm using @EnvironmentObject to bind my data to view. All works, but the Canvas crash and not show nothing. Why? struct CompetitionsListSwiftUIView : View { @EnvironmentObject var…
15
votes
1 answer

What is the correct syntax for using Publishers.debounce() in Swift Combine?

In Apple's 2019 WWDC video Swift Combine in Practice, they demonstrate using a debounce publisher to slow down the rate of messages. return $username .debounce(for: 0.5, scheduler: RunLoop.main) .removeDuplicates() …
kennyc
  • 5,490
  • 5
  • 34
  • 57
14
votes
3 answers

Published computed properties in SwiftUI model objects

Suppose I have a data model in my SwiftUI app that looks like the following: class Tallies: Identifiable, ObservableObject { let id = UUID() @Published var count = 0 } class GroupOfTallies: Identifiable, ObservableObject { let id = UUID() …
erdekhayser
  • 6,537
  • 2
  • 37
  • 69
14
votes
4 answers

Swift Combine: Buffer upstream values and emit them at a steady rate?

Using the new Combine framework in iOS 13. Suppose I have an upstream publisher sending values at a highly irregular rate - sometimes seconds or minutes may go by without any values, and then a stream of values may come through all at once. I'd like…
UberJason
  • 3,063
  • 2
  • 25
  • 50
14
votes
2 answers

Dismiss view from view model [MODAL PAGE]

I'm using swiftUI and combine, I'have some business logic in my VM. Some results have to dismiss my view. I'v used this one in some views : @Environment(\.presentationMode) var presentationMode:…
theMouk
  • 595
  • 1
  • 7
  • 21
14
votes
1 answer

How to tell SwiftUI views to bind to more than one nested ObservableObject

I have two classes nested in another class, which is an observable object in a SwiftUI view. Even though properties in the nested classes are declared as @Published, their values (when they change) do not update in the main view. A similar question…
mxt533
  • 225
  • 2
  • 6
13
votes
2 answers

ObservedObject view-model is still in memory after the view is dismissed

I am having some trouble with memory management in SwiftUI and Combine. For example, if I have a NavigationView and then navigate to a detail view with a TextField, and enter a value in the TextField and tap on the back button, next time I go to…
swifty
  • 971
  • 1
  • 8
  • 15
13
votes
4 answers

SwiftUI - memory leak in NavigationView

I am trying to add a close button to the modally presented View's navigation bar. However, after dismiss, my view models deinit method is never called. I've found that the problem is where it captures the self in navigationBarItem's. I can't just…
Vahagn Gevorgyan
  • 2,635
  • 19
  • 25
13
votes
4 answers

Combine framework retry after delay?

I see how to use .retry directly, to resubscribe after an error, like this: URLSession.shared.dataTaskPublisher(for:url) .retry(3) But that seems awfully simple-minded. What if I think that this error might go away if I wait awhile? I…
matt
  • 515,959
  • 87
  • 875
  • 1,141
13
votes
3 answers

How to: Using Combine to react to CoreData changes in the background

I want to achieve the following: Whenever someone triggers a CoreData save (ie. NSManagedObjectContextDidSave notification gets sent), I'd like to perform some background calculation based the changed NSManagedObject. Concrete example: Assume in a…
Nicolas
  • 755
  • 9
  • 22
13
votes
6 answers

How does Combine know an ObservableObject actually changed

The ObservableObject protocol defined by the Combine framework has an objectWillChange publisher property that lets you know when the properties of this object will change, meaning that, if subscribers to this publisher were to read its value when…
Pop Flamingo
  • 3,023
  • 2
  • 26
  • 64
13
votes
3 answers

Consume @Published values on the main thread?

Is there a way to specify that count should only publish on the main thread? I've seen docs that talk about setting up your Publisher using receive(on:), but in this case the @Publisher wrapper hides that logic. import SwiftUI import Combine class…
TALE
  • 960
  • 13
  • 22
13
votes
5 answers

Swift Combine alternative to Rx Observable.create

I have some code that is built using RxSwift, and I'm playing around with converting it to use Apple's Combine framework. One pattern which is very common is the use of Observable.create for one-shot observables (usually network requests). Something…
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
13
votes
3 answers

NSManagedObject changes do not trigger objectWillChange

I have a Core Data model with an entity generated into class Task. I am trying to get the Combine publisher objectWillChange from the NSManagedObject to send (automatically, without manual work), but it won't. The task entity has a name…
whistler
  • 540
  • 4
  • 14