combine

@State vs @ObservableObject - which and when?

≯℡__Kan透↙ 提交于 2021-01-15 22:12:23
问题 I'm currently getting familiar with SwiftUI and Combine frameworks. And I'm not really getting the difference between these two approaches. When we have to keep track of some data (say, a list of tasks), we can declare a @State variable, and it's change will automatically send notification and update current view. However, it looks like it can also be done this way: class TaskList: ObservableObject{ //a list that's going to be modified and updated on different occasions @Published var list:

How can I continue URLSession dataTaskPublisher or another Publisher after error?

孤者浪人 提交于 2021-01-07 02:43:44
问题 I have an app that needs to check a status on a server: every 30 seconds whenever the app enters the foreground I'm doing this by merging two publishers, then calling flatMap the merged publisher's output to trigger the API request. I have a function that makes an API request and returns a publisher of the result, also including logic to check the response and throw an error depending on its contents. It seems that once a StatusError.statusUnavailable error is thrown, the statusSubject stops

Write unit tests for ObservableObject ViewModels with Published results

北城以北 提交于 2021-01-05 07:23:25
问题 Today again one combine problem I currently run in and I hope that someone of you can help. How can normal unit tests be written for ObservableObjects classes which contain @Published attributes? How can I subscribe in my test to them to get the result object which I can assert? The injected mock for the web service works correctly, loadProducts() function set exactly the same elements from the mock in the fetchedProducts array. But I don't know currently how to access this array in my test

Write unit tests for ObservableObject ViewModels with Published results

淺唱寂寞╮ 提交于 2021-01-05 07:23:08
问题 Today again one combine problem I currently run in and I hope that someone of you can help. How can normal unit tests be written for ObservableObjects classes which contain @Published attributes? How can I subscribe in my test to them to get the result object which I can assert? The injected mock for the web service works correctly, loadProducts() function set exactly the same elements from the mock in the fetchedProducts array. But I don't know currently how to access this array in my test

how to use Publishers.CombineLatest to get 1 publisher

杀马特。学长 韩版系。学妹 提交于 2021-01-05 07:18:48
问题 I am trying to use 2 publishers and have them stream to 1 publisher that is mapped from both values. My code is: class ViewModel { let email = CurrentValueSubject<String, Never>("") lazy var isEmailValid = email.map { self.validateEmail(email: $0) } let password = CurrentValueSubject<String, Never>("") lazy var isPasswordCorrect = password.map { self.validatePassword(password: $0) } let canLogin: CurrentValueSubject<Bool, Never> private func validateEmail(email: String) -> Bool { return email

SwiftUI UIViewRepresentable UITextView Binding

大兔子大兔子 提交于 2020-12-29 16:56:42
问题 Multiline text input is currently not natively supported in SwiftUI (hopefully this feature is added soon!) so I've been trying to use the combine framework to implement a UITextView from UIKit which does support multiline input, however i've been having mixed results. This is the code i've created to make the Text view: struct MultilineTextView: UIViewRepresentable { @Binding var text: String func makeUIView(context: Context) -> UITextView { let view = UITextView() view.isScrollEnabled =

Combine onChange and onAppear events in SwiftUI view?

白昼怎懂夜的黑 提交于 2020-12-13 02:57:10
问题 I'm observe a property on a view using the onChange modifier. However, I'd also like the same piece of code to run on the initial value as well because sometimes the data is injected in the initializer or asynchronously loaded later. For example, I have a view that gets a model injected. Sometimes this model has data in it to begin with (like previews), or is asynchronously retrieved from the network. class MyModel: ObservableObject { @Published var counter = 0 } struct ContentView: View {

Is there a way to avoid using AnyPublisher/eraseToAnyPublisher all over the place?

梦想的初衷 提交于 2020-12-01 09:57:53
问题 I'm just learning how to use Combine. I have experience with Rx (RxSwift and RxJava) and I'm noticing that it's quite similar. However, one thing that is quite different (and kind of annoying) is that the Publisher protocol doesn't use generics for its Output and Failure types; it uses associated types instead. What this means is that I can't specify a polymorphic Publisher type (such as Publisher<Int, Error> ) and simply return any type that conforms to Publisher with those types. I need to

.sink is not returning the promise values from a Future Publisher

最后都变了- 提交于 2020-11-29 21:29:50
问题 I have this code in lrvViewModel.swift func getVerificationID (phoneNumber: String) -> Future<String?, Error> { return Future<String?, Error> { promise in PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber, uiDelegate: nil) { (verificationID, error) in if let e = error { promise(.failure(e)) return } print("verification worked") self.defaults.set(verificationID, forKey: "authVerificationID") return promise(.success(verificationID)) } } } and then i call and subscribe to the Publisher

.sink is not returning the promise values from a Future Publisher

岁酱吖の 提交于 2020-11-29 21:27:05
问题 I have this code in lrvViewModel.swift func getVerificationID (phoneNumber: String) -> Future<String?, Error> { return Future<String?, Error> { promise in PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber, uiDelegate: nil) { (verificationID, error) in if let e = error { promise(.failure(e)) return } print("verification worked") self.defaults.set(verificationID, forKey: "authVerificationID") return promise(.success(verificationID)) } } } and then i call and subscribe to the Publisher