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
8
votes
3 answers

Convert URLSession.DataTaskPublisher to Future publisher

How to convert URLSession.DataTaskPublisher to Future in Combine framework. In my opinion, the Future publisher is more appropriate here because the call can emit only one response and fails eventually. In RxSwift there is helper method like…
mkowal87
  • 596
  • 4
  • 19
8
votes
2 answers

Reference EnvironmentObject in ObservableObject

I have a LoginView that shows a RegisterView if the user is not logged in, and a ContentView if he is logged in: struct LoginView: View { @EnvironmentObject var userManager: UserManager var body: some View { Group { if…
Ivan C Myrvold
  • 680
  • 7
  • 23
8
votes
2 answers

Binding to a read-only property in SwiftUI

I have a model type which looks like this: enum State { case loading case loaded([String]) case failed(Error) var strings: [String]? { switch self { case .loaded(let strings): return strings default: return…
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
8
votes
1 answer

iOS SwiftUI: ObservableObject from parent to child

Problem: Passing down the view model and modifying it from the children, won't refresh the parent. What I want: Whenever I modify the view model from the child view, the parent refresh too. Problem description: I'm looking for a way to pass down a…
Andrea Miotto
  • 7,084
  • 8
  • 45
  • 70
8
votes
1 answer

SwiftUI @FetchRequest Core Data Changes to Relationships Don't Refresh

A Core Data model with entity Node having name, createdAt, to-many relationship children and to-one relationship parent (both optional). Using CodeGen Class Definition. Using a @FetchRequest with a predicate of parent == nil, it's possible to grab…
user192742
  • 81
  • 1
  • 3
8
votes
1 answer

In a UIViewControllerRepresentable, how can I pass an ObservedObject's value to the view controller and update it every time the value changes?

I have a UIViewControllerRepresentable struct that is subscribed to an ObservableObject, like this: struct ViewControllerWrapper: UIViewControllerRepresentable { @ObservedObject var chartVM = ChartViewModel() typealias…
Nolan Mulder
  • 81
  • 1
  • 5
8
votes
2 answers

What is the best way to handle errors in Combine?

I am trying to decode the downloaded JSON into a structure with the following code. static func request(url: URL) -> AnyPublisher { return URLSession.shared.dataTaskPublisher(for: url) .map { $0.data } …
omattyao
  • 95
  • 2
  • 6
8
votes
3 answers

Swift Combine: Using timer publisher in an observable object

Before this question gets marked as duplicate of this other question, I am trying to understand how the publisher works as it behaves in a way I do not expect. Using the same example as the answer from the question previously stated: // Let's…
rodrigoelp
  • 2,550
  • 1
  • 19
  • 29
8
votes
1 answer

Why doesn't URLSession.DataTaskPublisher ever publish values?

In Xcode 11 beta 5 or 6 my existing code that relied on URLSession.DataTaskPublisher stopped working. It seems like DataTaskPublisher is never publishing any values but I can't work out why. I've tried with .sink and .handleEvents as subscribers.…
Belle B. Cooper
  • 375
  • 5
  • 13
8
votes
2 answers

Translating async method into Combine

I'm trying to wrap my head around Combine. Here's a method I want to translate into Combine, so that it would return AnyPublisher. func getToken(completion: @escaping (Result) -> Void) { dispatchQueue.async { do { …
swasta
  • 846
  • 8
  • 25
8
votes
1 answer

Animation on Toggle Change with SwiftUI

I am building a simple settings screen. When then first setting is activated, the Speed Control appears. When it's turned off, the Speed Control disappears. Based on what I know about SwiftUI, this should automatically animate based on the code…
gohnjanotis
  • 6,513
  • 6
  • 37
  • 57
8
votes
1 answer

Can anyone explain Swift Combine's Subject.eraseToAnySubject() method and where it should be used?

I can see that Subject.eraseToAnySubject() returns the concrete Subject type AnySubject. I'm assuming this is using a type eraser pattern. However, the apple docs provide almost no details:…
Andrew Paul Simmons
  • 4,334
  • 3
  • 31
  • 39
7
votes
1 answer

Is there a way to access enclosing instance `ObservableObject` to call `objectWillChange.send()` from anywhere in a property wrapper

I'm trying to make a property wrapper similar to Combine's Published one(for my project needs), but with ability to modify the wrapped property by sending a value to a publisher, stored in projectedValue, like this: // in class @PublishedMutable var…
7
votes
1 answer

How do I make my own focusable view in SwiftUI using FocusState?

So I want to implement a custom control as a UIViewRepresentable which correctly handles focus using an @FocusState binding. So I want to be able to manage the focus like so: struct MyControl: UIViewRepresentable { ... } struct Container: View { …
sak
  • 2,612
  • 24
  • 55
7
votes
3 answers

Detail View is not updated when the model is updated (Using List) SwiftUI

I am using List to show the models in the main view. When I update the model in detail view it is not updated in detail view. When I don't use List, detail view is updated. What am I missing for List? struct Person: Identifiable { var id: UUID …
egeeke
  • 753
  • 1
  • 6
  • 18