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

SwiftUI passing data between multiple classes

After scouring Apple's SwiftUI docs, the web and stackoverflow, I can't seem to figure out how to use multiple classes and passing EnviromentObject data between them. All the articles on SwiftUI's EnvironmentObject, ObservableObjects, Bindings show…
Ryan
  • 10,798
  • 11
  • 46
  • 60
7
votes
4 answers

Updating a @Published variable based on changes in an observed variable

I have an AppState that can be observed: class AppState: ObservableObject { private init() {} static let shared = AppState() @Published fileprivate(set) var isLoggedIn = false } A View Model should decide which view to show based on…
Benno Kress
  • 2,637
  • 27
  • 39
7
votes
1 answer

Swift Combine - @Published property Array

I am currently doing a project using SwiftUI and Combine. I'm on Xcode11 Beta 5. I would like to fetch my Github repositories, display them and then be able to bookmark some of them. I'm able to fetch them and display them. I'm using Combine with…
Hurobaki
  • 3,728
  • 6
  • 24
  • 41
7
votes
2 answers

Skeleton Example for Swift Combine Publisher-Subscriber

As I port some Objective-C code to Swift, I'm trying to better understand the new Combine framework and how I can use it to re-create a common design pattern. In this case, the design pattern is a single object (Manager, Service, etc) that any…
kennyc
  • 5,490
  • 5
  • 34
  • 57
7
votes
1 answer

How to change thread using combine Publisher?

I am using Combine and SwiftUI to do some async stuff, the point is that I don't know how to receive the response from the asynchronous operation in the main thread. The apple doc says that it can be used the RunLoop.main, but currently in Swift 5.0…
Alberto Penas
  • 549
  • 2
  • 6
  • 10
7
votes
2 answers

Combine in Playgrounds

I'm inspecting Combine, a new framework by Apple. I created a playground, ran it in macOS Mojave 10.14.5 and Xcode 11.0 beta (11M336w). Here is my code: import Combine struct Article: Identifiable { var id: Int var title: String } final…
Artem Novichkov
  • 2,356
  • 2
  • 24
  • 34
6
votes
1 answer

Inconsistent behavior of map function with Empty value in Supply and plain Array?

Recently, I have been learning Swift's Combine framework. In Apple's words: The Combine framework provides a declarative approach for how your app processes events. Rather than potentially implementing multiple delegate callbacks or completion…
chenyf
  • 5,048
  • 1
  • 12
  • 35
6
votes
1 answer

What is difference between Combine and RxSwift?

I wonder if there is an excellent document to show the difference between Combine and RxSwift? It's good for me to quickly learn the Combine because I already had good knowledge about RxSwift
Mark G
  • 124
  • 3
  • 5
6
votes
1 answer

@StateObject vs @ObservedObject when passed externally but owned by the view

Based on this answer: What is the difference between ObservedObject and StateObject in SwiftUI And the Apple documentation code here: https://developer.apple.com/documentation/swiftui/managing-model-data-in-your-app In SwiftUI app, a @StateObject…
Richard Topchii
  • 7,075
  • 8
  • 48
  • 115
6
votes
1 answer

SwiftUI Combine - How to test waiting for a publisher's async result

I am listening for changes of a publisher, then fetching some data asynchronously in my pipeline and updating the view with the result. However, I am unsure how to make this testable. How can I best wait until the expectation has been…
Ryan
  • 145
  • 1
  • 6
6
votes
2 answers

How to receive @Published var in main thread

I keep the Model as a Published var in the ViewModel and Observe it from the View. When the model process goes into a background thread, if you publish the model value, the Xcode thread checker will react. Publishing changes from background threads…
user38155
  • 61
  • 3
6
votes
2 answers

How do you run a Swift Combine Publisher for a certain amount of time?

I have a publisher when the sink, scans for a list of wifi. I only want to scan for about 10 seconds and stop. Is there a way to do this within the publisher chain of calls?
Unikorn
  • 1,140
  • 1
  • 13
  • 27
6
votes
1 answer

SwiftUI List Repeating ID Value

I am working on a SwiftUI project that pulls data from Firebase Firestore using Combine. Each user has the ability to create "Offers" in the app. In order to list their offers on their account page I am using onAppear to pass the currentUserUid to…
jonthornham
  • 2,881
  • 4
  • 19
  • 35
6
votes
1 answer

How to bind SwiftUI and UIViewController behavior

I have a UIKit project with UIViewControllers, and I'd like to present an action sheet built on SwiftUI from my ViewController. I need to bind the appearance and disappearance of the action sheet back to the view controller, enabling the view…
Bruno Machado - vargero
  • 2,690
  • 5
  • 33
  • 52
6
votes
4 answers

SwiftUI Combine: Nested Observed-Objects

What is the best approach to have swiftUI still update based on nested observed objects? The following example shows what I mean with nested observed objects. The balls array of the ball manager is a published property that contains an array of…
Leo
  • 1,508
  • 13
  • 27