combine

how to populate picker from ViewModel object, setting to initial state of first element and handle actions on selection of picker item

我怕爱的太早我们不能终老 提交于 2020-03-25 19:00:13
问题 I have a view class showing list of items coming from ViewModel class, in picker. Initial state of this picker is first element from the array of objects of the viewModel class. On selection of item from picker, I want to do different actions in that view - 1. send the object info to different screen on button click. 2. display information with respected to selected object from picker. import SwiftUI import Combine struct SetConfiguration: View { @ObservedObject var profileListVM :

Using Combine's Future to replicate async await in Swift

若如初见. 提交于 2020-03-25 18:47:11
问题 I am creating a Contact Class to fetch user's phoneNumbers asynchronously. I created 3 functions that leveraged on the new Combine framework's Future. func checkContactsAccess() -> Future<Bool, Never> { Future { resolve in let authorizationStatus = CNContactStore.authorizationStatus(for: .contacts) switch authorizationStatus { case .authorized: return resolve(.success(true)) default: return resolve(.success(false)) } } } func requestAccess() -> Future<Bool, Error> { Future { resolve in

View doesn't get updated when using ObservableObject

别说谁变了你拦得住时间么 提交于 2020-03-23 12:01:45
问题 I'm trying to build an Instagram clone app using SwiftUI. I'm fetching the data through Firebase and trying to achieve a UI update every time the data in the server changes. For some reason, when I first open the app and fetch the data, the body of my view gets called, but the UI doesn't change. I even put a breakpoint and saw the body gets called and contains the correct information, it's just the UI which doesn't get updated. I have a few tabs in my app, and when I switch to another tab

Swift Combine: `append` which does not require output to be equal?

随声附和 提交于 2020-03-04 16:01:10
问题 Using Apple's Combine I would like to append a publisher bar after a first publisher foo has finished (ok to constrain Failure to Never ). Basically I want RxJava's andThen. I have something like this: let foo: AnyPublisher<Fruit, Never> = /* actual publisher irrelevant */ let bar: AnyPublisher<Fruit, Never> = /* actual publisher irrelevant */ // A want to do concatenate `bar` to start producing elements // only after `foo` has `finished`, and let's say I only care about the // first element

Combine: Publisher sometimes loses value and completes

谁都会走 提交于 2020-02-24 11:42:19
问题 I have a simple Deferred Publisher that reads data from disk and I display the data in a SwiftUI List , the Publisher works well most of the time, but sometimes it doesn't behave well, it just loses its value (which's an array of Model objects) and completes with finished message. I've tried a workaround mentioned here to use the buffer operator to keep the value in buffer because I believe the Combine's Publisher by design won't pass the data downstream if there is no demand requested by

Combine framework serialize async operations

丶灬走出姿态 提交于 2020-02-21 13:08:49
问题 How do I get the asynchronous pipelines that constitute the Combine framework to line up synchronously (serially)? Suppose I have 50 URLs from which I want to download the corresponding resources, and let's say I want to do it one at a time. I know how to do that with Operation / OperationQueue, e.g. using an Operation subclass that doesn't declare itself finished until the download is complete. How would I do the same thing using Combine? At the moment all that occurs to me is to keep a

SwiftUI - propagating change notifications through nested reference types

五迷三道 提交于 2020-02-16 04:55:08
问题 I'd like to extend ObservableObject behavior in SwiftUI to nested classes, and I'm looking for the proper way to do it. It can be done "manually" with Combine, but I imagine there's a much cleaner way to do it using SwiftUI, and I'm hoping you can point me in the right direction. Here's what I mean… Below is a typical application of ObservableObject to make a View dynamically respond to changes to a reference type. Tapping the button toggles the showText value, which makes the text appear

Publisher inside a Publisher does not trigger SwiftUi re-render

做~自己de王妃 提交于 2020-02-05 04:26:11
问题 I made a "State" Object for my App in EnvironmentObject Like this: class AppState: ObservableObject { @Published var counter = Counter() } To add this to my App I use: window.rootViewController = UIHostingController(rootView: contentView.environmentObject(state)) The counter is a background task class Counter: ObservableObject { @Published var ammount: Double var timer = Timer() init() { self.ammount = 0.0 self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector

Subscribing to changes to @Published

假装没事ソ 提交于 2020-02-05 02:37:27
问题 I am trying to bind the value of query to a search box sitting in a SwiftUI view. class DataSet: ObservedObject { ... @Published var query: String = "" init() { let sub = AnySubscriber<String, Never>( receiveSubscription: nil, receiveValue: { query in print(query) return .unlimited }) self.$query.subscribe(sub) } ... } When the user changes the value of the query I'd like to filter some other property in my ObservedObject . Yet I cannot find anywhere in the documentation how do I subscribe to

Swift Combine: Check if Subject has observer?

北慕城南 提交于 2020-01-24 23:10:48
问题 In RxSwift we can check if a *Subject has any observer, using hasObserver, how can I do this in Combine on e.g. a PassthroughSubject ? 回答1: No one time needed this... Apple does not provide this by API, and, actually, I do not recommend such thing, because it is like manually checking value of retainCount in pre-ARC Objective-C for some decision in code. Anyway it is possible. Let's consider it as a lab exercise. Hope someone find this helpful. Disclaimer: below code was not tested with all