combine

Optional linking for Swift Combine.framework in Xcode 11

耗尽温柔 提交于 2019-12-21 04:09:13
问题 Our application supports iOS 11 and higher. In iOS 13 we use SwiftUI + Combine we wrap import of SwiftUI or Combine framework with correspondent check #if canImport(SwiftUI) or #if canImport(Combine) . If we run our app from Xcode 11 under iOS 12 we have error dyld: Library not loaded: /System/Library/Frameworks/Combine.framework/Combine We fixed same issue for SwiftUI by linking it optionally. But we can't make same for Combine as it can not be even selected for linking 回答1: You can add the

URLSession.shared.dataTaskPublisher not working on IOS 13.3

假装没事ソ 提交于 2019-12-19 10:16:13
问题 When trying to make a network request, I'm getting an error finished with error [-999] Error Domain=NSURLErrorDomain Code=-999 "cancelled" If I use URLSession.shared.dataTask instead of URLSession.shared.dataTaskPublisher it will work on IOS 13.3. Here is my code : return URLSession.shared.dataTaskPublisher(for : request).map{ a in return a.data } .decode(type: MyResponse.self, decoder: JSONDecoder()) .receive(on: DispatchQueue.main) .eraseToAnyPublisher() This code worked on IOS 13.2.3. 回答1:

How to update @FetchRequest, when a related Entity changes in SwiftUI?

淺唱寂寞╮ 提交于 2019-12-19 04:09:08
问题 In a SwiftUI View i have a List based on @FetchRequest showing data of a Primary entity and the via relationship connected Secondary entity. The View and its List is updated correctly, when I add a new Primary entity with a new related secondary entity. The problem is, when I update the connected Secondary item in a detail view, the database gets updated, but the changes are not reflected in the Primary List. Obviously, the @FetchRequest does not get triggered by the changes in another View.

SwiftUI @Binding doesn't refresh View

☆樱花仙子☆ 提交于 2019-12-19 04:06:45
问题 I have a simple master/detail interface where the detail view modifies an item in an array. Using the below, the model is updated properly, but SwiftUI doesn't refresh the View to reflect the change. Model: struct ProduceItem: Identifiable { let id = UUID() let name: String var inventory: Int } final class ItemStore: BindableObject { var willChange = PassthroughSubject<Void, Never>() var items: [ProduceItem] { willSet { willChange.send() } } init(_ items: [ProduceItem]) { self.items = items }

Fixing Combine error “cannot invoke 'debounce' with an argument list of type '(for: Double, scheduler: RunLoop)'”

我与影子孤独终老i 提交于 2019-12-13 02:47:36
问题 In the WWDC beta, I see an error when using the debounce function from Combine, like this debounce(for: 0.5, scheduler: RunLoop.main) Error is cannot invoke 'debounce' with an argument list of type '(for: Double, scheduler: RunLoop)_ I guess the build is not as recent as the Combine presentations themselves, which do use this construction. Has anyone got a workaround? 来源: https://stackoverflow.com/questions/56564967/fixing-combine-error-cannot-invoke-debounce-with-an-argument-list-of-type-f

Swift Combine: Using timer publisher in an observable object

江枫思渺然 提交于 2019-12-12 09:19:53
问题 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 define the view model with my view... import Combine import SwiftUI class TimerViewModel: ObservableObject { private let cancellable: AnyCancellable? let intervalPublisher = Timer.TimerPublisher( interval: 1.0, runLoop: .main, mode: .default) init() {

Limit TextField to x amount of characters using SwiftUI

落爺英雄遲暮 提交于 2019-12-11 16:29:28
问题 Using iOS13.2, Swift-5.1.2, Xcode-11.2, I try the following: I want to use a TextField. The user shall only be able to enter x-amount of characters into the TextField. My code looks as follows: import Combine import SwiftUI class Entry: ObservableObject { @Published var entry = "" { didSet { entry = String(entry.prefix(6)) // trying to limit to 6 characters } } } And in the above code, there is already the exception line. I can see that the didSet{...} is wrong (since we end up in an endless

Swift Combine: How can I convert `AnyPublisher<[Foo], *>` to `AnyPublisher<Foo, *>`?

好久不见. 提交于 2019-12-11 05:59:12
问题 How can I convert a publisher of array a certain element, to just a publisher of said element (but with more events)? e.g. how can I convert AnyPublisher<[Int], Never> to AnyPublisher<Int, Never> ? I think maybe what RxSwift offers with its from operator is similar to what I want to do. I guess I want the inverse of Combine collect? 回答1: Here is the code: func example(publisher: AnyPublisher<[Foo], Never>) -> AnyPublisher<Foo, Never> { return publisher .map { $0.publisher } .switchToLatest()

SwiftUI and Combine not working smoothly when downloading image asynchrously

烂漫一生 提交于 2019-12-11 05:07:06
问题 When I tried to use SwiftUI & Combine to download image asynchrously, it works fine. Then, I try to implement this into a dynamic list, and I found out there is only one row(the last row) will be show correctly, images in other cells are missing. I have trace the code with breakpoints and I'm sure the image download process is success in others, but only the last row will trigger the @ObjectBinding to update image. Please check my sample code and let me know if there's any wrong. Thanks!

Swift Combine: What are those multicast functions for and how do I use them?

跟風遠走 提交于 2019-12-10 09:25:15
问题 Struggling with some combine problems I came across the "Working with Multiple Subscribers" section in https://developer.apple.com/documentation/combine/publisher : func multicast<S>(() -> S) -> Publishers.Multicast<Self, S> func multicast<S>(subject: S) -> Publishers.Multicast<Self, S> However, when I tried to confirm my assumption that multicast would be needed when sending to multiple subscribers, I found out this is not necessary when trying on this playground code (modified from https:/