rx-swift

Best data-binding practice in Combine + SwiftUI?

限于喜欢 提交于 2020-05-10 07:29:10
问题 In RxSwift it's pretty easy to bind a Driver or an Observable in a View Model to some observer in a ViewController (i.e. a UILabel ). I usually prefer to build a pipeline, with observables created from other observables , instead of "imperatively" pushing values, say via a PublishSubject ). Let's use this example: update a UILabel after fetching some data from the network RxSwift + RxCocoa example final class RxViewModel { private var dataObservable: Observable<Data> let stringDriver: Driver

RxSwift - how to chain observables sequentially

烂漫一生 提交于 2020-04-29 10:00:32
问题 Assume I have array of Ints: var items = [1, 2, 3, 4, 5] and a function that takes Int argument and basing on it sends network request: func sendRequest(argument: Int) -> Observable<Void> { // sends network request ... } I want to send network requests for each element of items array, but I want to do it sequentially, send next request only once previous one is finished. My first attempt was something like this: let observables = items.map{ [weak self] argument in (self?.sendRequest(argument:

How do you get a signal every time a UITextField text property changes in RxSwift

非 Y 不嫁゛ 提交于 2020-03-18 03:30:19
问题 How do you get a signal from programmatically made changes to UITextField text property? By using rx.text only reports a signal when the user input the text by keyboard. If you set textField.text programmatically, the signal doesn't fire. This is how I'm using rx.text (self.phoneNumberTextField.rx.text).orEmpty.subscribe(onNext: { [weak self] (phone) in // do something }).addDisposableTo(disposeBag) self.phoneNumberTextField.text = "this is for test" Thank you! 回答1: I had the same issues,

RxSwift: Use Zip with different type observables

自作多情 提交于 2020-02-26 13:02:13
问题 I'm using RxSwift 2.0.0-beta How can I combine 2 observables of different types in a zip like manner? // This works [just(1), just(1)].zip { intElements in return intElements.count } // This doesn't [just(1), just("one")].zip { differentTypeElements in return differentTypeElements.count } The current workaround I have is to map everything to an optional tuple that combines the types, then zips optional tuples into a non-optional one. let intObs = just(1) .map { int -> (int: Int?, string:

Is there some sort of Priority operator on RxSwift?

别等时光非礼了梦想. 提交于 2020-02-05 02:30:12
问题 I have a textfield that has 2 rules for validation: minimum amount of chars and alphanumerical chars. I want to be able to represent to the user what he's doing wrong in an error label but the problem is that if I bind the textfield to both rules it can be creepy because once one rule gets approved the ui does a little flickering from the color of the separator for example changing from red to green to red because of the other validation failing. I'd like to know if there's a way to

Is possible to do a custom binder on an array of input fields using RxSwift?

时光总嘲笑我的痴心妄想 提交于 2020-01-25 10:13:51
问题 I have two input fields, one for password and another one to confirm the password. I want to create a custom binder that will allow me to compare the value of both input fields but also validate minimum amount of chars. I had a pretty similar question but not regarding comparing two different fields (Is there some sort of Priority operator on RxSwift?) and based on the answer for that previous question I've been trying to do something like this: enum PasswordCreateValidation { case valid case

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

Swift Combine: Check if Subject has observer?

不羁的心 提交于 2020-01-24 23:09:05
问题 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

How to transform rx_tap of UIButton to a network request directly without sending the request in a nested subscribe?

别等时光非礼了梦想. 提交于 2020-01-23 03:58:32
问题 Suppose that I have a UIButton loginButton , I want to send a network request while tapping the button with the following code: override func viewDidLoad() { super.viewDidLoad() let session = self.session // NSURLSession loginButton.rx_tap.subscribeNext { [unowned self] in session.rx_response(myRequest).subscribe { event in switch event { case .Next(let data, let response): // Handling Response case .Error(let error): // Handling Error default: return } }.addDisposableTo(disposeBag) }

Observe array in Swift 3 using RxSwift

梦想的初衷 提交于 2020-01-22 12:28:10
问题 To create an observable array using RxSwift in Swift 2, I use to do this: [1, 2, 3].toObservable().subscribeNext { print($0) } But in Swift 3, it doesn't work anymore, I got this error: Value of type '[Int]' has no member 'toObservable' How can I create an RxSwift observable array from a swift array? 回答1: In Swift 3 using RxSwift 3.0 I will do that like this: var array: Variable<[Int]> = Variable([1, 2, 3]) array.asObservable().subscribe(onNext: { updatedArray in print(updatedArray) }) array