combine

How to process an array of task asynchronously with swift combine

匆匆过客 提交于 2020-05-28 06:48:48
问题 I have a publisher which takes a network call and returns an array of IDs. I now need to call another network call for each ID to get all my data. And I want the final publisher to have the resulting object. First network result: "user": { "id": 0, "items": [1, 2, 3, 4, 5] } Final object: struct User { let id: Int let items: [Item] ... other fields ... } struct Item { let id: Int ... other fields ... } Handling multiple network calls: userPublisher.flatMap { user in let itemIDs = user.items

Combine framework retry after delay?

偶尔善良 提交于 2020-05-27 06:34:45
问题 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 could insert a .delay operator, but then the delay operates even if there is no error. And there doesn't seem to be a way to apply an operator conditionally (i.e. only when there's an error). I see how I could work around this by writing a RetryWithDelay

Combine framework retry after delay?

♀尐吖头ヾ 提交于 2020-05-27 06:33:17
问题 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 could insert a .delay operator, but then the delay operates even if there is no error. And there doesn't seem to be a way to apply an operator conditionally (i.e. only when there's an error). I see how I could work around this by writing a RetryWithDelay

Swift Combine: subsequent Publisher that consumes other Publishers (using CombineLatest) doesn't “fire”

半城伤御伤魂 提交于 2020-05-25 08:06:27
问题 I am trying to replicate the "Wizard School Signup"-example which was given in the WWDC 2019 session "Combine in Practice" https://developer.apple.com/videos/play/wwdc2019/721/ starting at 22:50 using SwiftUI (as opposed to UIKit, which was used during the session). I have created all the publishers from the example: validatedEMail, validatedPassword and validatedCredentials. While validatedEMail and validatedPassword work just fine, validatedCredentials, which consumes both publishers using

What causes updateUIView() to be called in this code?

て烟熏妆下的殇ゞ 提交于 2020-05-17 07:53:05
问题 I understand that the change of the @State variable notifies the @Binding that the state has changed but what then causes the updateUIView() method to be called? There is obviously some hidden connection between the @Binding and the call, but how does that work? // Experiment_Map_View.swift import SwiftUI import MapKit struct Experiment_Map_View: UIViewRepresentable { @Binding var test: Bool func updateUIView(_ uiView: MKMapView, context: Context) { print("updateUIView") print(test) } func

How do I access data from a child view as the parent view at any time in SwiftUI?

China☆狼群 提交于 2020-05-16 22:01:15
问题 I'm new to SwiftUI and understand that I may need to implement EnvironmentObject in some way, but I'm not sure how in this case. This is the Trade class class Trade { var teamsSelected: [Team] init(teamsSelected: [Team]) { self.teamsSelected = teamsSelected } } This is the child view. It has an instance trade from the Trade class. There is a button that appends 1 to array teamsSelected . struct TeamRow: View { var trade: Trade var body: some View { Button(action: { self.trade.teamsSelected

Swift Combine alternative to Rx Observable.create

允我心安 提交于 2020-05-15 04:24:26
问题 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 like this: func loadWidgets() -> Observable<[Widget]> { return Observable.create { observer in // start the request when someone subscribes let loadTask = WidgetLoader.request("allWidgets", completion: { widgets in // publish result on success

Observing Binding or State variables

心不动则不痛 提交于 2020-05-13 23:28:03
问题 I'm looking for a way of observing @State or @Binding value changes in onReceive . I can't make it work, and I suspect it's not possible, but maybe there's a way of transforming them to Publisher or something while at the same time keeping the source updating value as it's doing right now? Below you can find some context why I need this: I have a parent view which is supposed to display half modal based on this library: https://github.com/AndreaMiotto/PartialSheet For this purpose, I've

How to replicate PromiseKit-style chained async flow using Combine + Swift

做~自己de王妃 提交于 2020-05-13 06:59:55
问题 I was using PromiseKit successfully in a project until Xcode 11 betas broke PK v7. In an effort to reduce external dependencies, I decided to scrap PromiseKit. The best replacement for handling chained async code seemed to be Futures using the new Combine framework. 1) Is this dumb? 2) I am struggling to replicate the simple PK syntax using Combine ex. simple PromiseKit chained async call syntax getAccessCodeFromSyncProvider.then{accessCode in startSync(accessCode)}.then

Swift Combine properties inheritance throws 'Fatal error: Call of deleted method' on Xcode 11.4 beta 2 / iOS 13.4

早过忘川 提交于 2020-05-11 06:43:41
问题 I'm trying to using Swift Combine to get the changed event of a property. I have this class that publish the isLogged property class CurrentUser: Account { static let me = CurrentUser() //Singleton @Published var isLogged: Bool = false } that inherit from this other class that publish the profileImageVersion property class Account { @Published var profileImageVersion: String? init(){ self.profileImageVersion = "" } } I'm trying to subscribe to the published inherit profileImageVersion