combine

Performing side effects on Publisher

自闭症网瘾萝莉.ら 提交于 2020-07-04 03:26:25
问题 I'm trying to perform side effect on Publisher , and I can't find any operator which will allow me to do something like this. To be precise, I'm looking for a concept similar to RxSwift's do(on:) . Here's what I'm trying to do. I'm writing a reactive wrapper for a delegate provided by a 3rd party SDK. It's and SDK for interacting with BLE devices. I have methods like startScanning and stopScanning and what I want to achieve is to react to signals and call stopScanning as a side effect of any

How to mock URLSession.DataTaskPublisher

喜欢而已 提交于 2020-06-27 12:06:16
问题 How can I mock URLSession.DataTaskPublisher ? I have a class Proxy that require to inject a URLSessionProtocol protocol URLSessionProtocol { func loadData(from url: URL) -> URLSession.DataTaskPublisher } class Proxy { private let urlSession: URLSessionProtocol init(urlSession: URLSessionProtocol) { self.urlSession = urlSession } func get(url: URL) -> AnyPublisher<Data, ProxyError> { // Using urlSession.loadData(from: url) } } This code was originally used with the traditional version of

SwiftUI - memory leak in NavigationView

我的未来我决定 提交于 2020-06-22 11:53:33
问题 I am trying to add a close button to the modally presented View's navigation bar. However, after dismiss, my view models deinit method is never called. I've found that the problem is where it captures the self in navigationBarItem 's. I can't just pass a weak self in navigationBarItem 's action, because View is a struct, not a class. Is this a valid issue or just a lack of knowledge? struct ModalView: View { @Environment(\.presentationMode) private var presentation: Binding<PresentationMode>

How to create a custom chain in Swift Combine?

自作多情 提交于 2020-06-17 15:56:05
问题 I'm trying to create a LocationManager wrapper for Combine. I have a publisher and some functions that trigger the publisher. However, I'd like to combine them in one with a custom command. Here's what I got so far: @available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *) public class LocationProxy: NSObject { private lazy var manager = CLLocationManager() private static let authorizationSubject = PassthroughSubject<Bool, Never>() public private(set) lazy var authorizationPublisher: AnyPublisher

View refreshing not triggered when ObservableObject is inherited in SwiftUI

杀马特。学长 韩版系。学妹 提交于 2020-06-17 07:24:10
问题 ContentView2 view is not refreshed when model.value changes, if Model conforms to ObservableObject directly instead of inheriting SuperModel then it works fine class SuperModel: ObservableObject { } class Model: SuperModel { @Published var value = "" } struct ContentView2: View { @ObservedObject var model = Model() var body: some View { VStack { Text(model.value) Button("change value") { self.model.value = "\(Int.random(in: 1...10))" } } } } 回答1: Here is working variant of your example. See

View refreshing not triggered when ObservableObject is inherited in SwiftUI

匆匆过客 提交于 2020-06-17 07:24:07
问题 ContentView2 view is not refreshed when model.value changes, if Model conforms to ObservableObject directly instead of inheriting SuperModel then it works fine class SuperModel: ObservableObject { } class Model: SuperModel { @Published var value = "" } struct ContentView2: View { @ObservedObject var model = Model() var body: some View { VStack { Text(model.value) Button("change value") { self.model.value = "\(Int.random(in: 1...10))" } } } } 回答1: Here is working variant of your example. See

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

雨燕双飞 提交于 2020-06-16 05:28:05
问题 I have a UIViewControllerRepresentable struct that is subscribed to an ObservableObject, like this: struct ViewControllerWrapper: UIViewControllerRepresentable { @ObservedObject var chartVM = ChartViewModel() typealias UIViewControllerType = ViewController func makeUIViewController(context: Context) -> ViewController { let lineChartView = LineChartView() let vc = ViewController(lineChartView: lineChartView) return vc } func updateUIViewController(_ uiViewController: ViewController, context:

SwiftUI two-way binding to value inside ObservableObject inside enum case

假如想象 提交于 2020-06-16 02:22:07
问题 I am trying to observe changes of a bool value contained in an ObservableObject which is a value in an enum case. Here is an example of what I am trying to achieve but with the current approach I receive the error Use of unresolved identifier '$type1Value' . import SwiftUI import Combine class ObservableType1: ObservableObject { @Published var isChecked: Bool = false } enum CustomEnum { case option1(ObservableType1) } struct Parent: View { var myCustomEnum: CustomEnum var body: AnyView {

iOS Swift Combine: cancel a Set<AnyCancellable>

若如初见. 提交于 2020-06-14 04:37:05
问题 If I have stored a cancellable set into a ViewController: private var bag = Set<AnyCancellable>() Wich contains multiple subscription. 1 - Should I cancel subscription in deinit? or it does the job automatically? 2 - If so, how can I cancel all the stored subscriptions? bag.removeAll() is enough? or should I iterate through the set and cancel all subscription one by one? for sub in bag { sub.cancel() } Apple says that the subscription is alive until the stored AnyCancellable is in memory. So

Somehow combine with search controller not working, any idea?

一曲冷凌霜 提交于 2020-06-01 07:35:27
问题 For some reason when I type in the search field it does not print out Xcode console the "str". What am I missing here? I followed his tutorial https://www.letsbuildthatapp.com/course_video?id=5232 import UIKit class SearchViewController: UIViewController { let searchController = UISearchController(searchResultsController: nil) var sink: Any? override func viewDidLoad() { super.viewDidLoad() setupSearchBarListener() navigationItem.searchController = searchController navigationController?