rx-swift

RxSwift/RxCocoa: prevent UITextField from having more than … characters

随声附和 提交于 2019-11-30 18:04:52
I would like to have a UITextField configured with RxSwift/RxCocoa so that it only contains up to ... characters. I do not want to use the UITextFieldDelegate for this but would love to achieve this with RxSwift/RxCocoa. Is there a way to do this? Sure: textField.rx.controlEvent(.editingChanged).subscribe(onNext: { [unowned self] in if let text = self.textField.text { self.textField.text = String(text.prefix(40)) } }).disposed(by: disposeBag) In this example, the textfield is limited to 40 characters. Edit: Keeping the previous value when the limit is reached. textField.rx.text.orEmpty .scan("

RxSwift - Debounce/Throttle “inverse”

梦想与她 提交于 2019-11-30 17:08:52
Let's say I have an instant messaging app that plays a beep sound every time a message arrives. I want to debounce the beeps, but I'd like to play the beep sound for the first message arrived and not for the following ones (in a timespan of, say, 2 seconds). Another example might be: my app sends typing notifications (so the user I'm chatting with can see that I'm typing a message). I want to send a typing notification when I start typing, but only send new ones in X-seconds intervals, so I don't send a typing notification for every character I type. Does this make sense? Is there an operator

Two way binding in RxSwift

白昼怎懂夜的黑 提交于 2019-11-30 13:39:30
I read the two way binding operator in sample code of RxSwift. func <-> <T>(property: ControlProperty<T>, variable: Variable<T>) -> Disposable { let bindToUIDisposable = variable.asObservable() .bindTo(property) let bindToVariable = property .subscribe(onNext: { n in variable.value = n }, onCompleted: { bindToUIDisposable.dispose() }) return StableCompositeDisposable.create(bindToUIDisposable, bindToVariable) } When property changed, it will notify variable, and set the variable's value, while the variable's value is set, it will notify the property. I think it will lead to endless loop...

How do I create an observable of an array from an array of observables?

北城以北 提交于 2019-11-30 11:50:13
问题 I have an array of Thing objects that I want to convert to ConvertedThing objects, using an asynchronous function that returns Observable<ConvertedThing> . I'd like to create an Observable<[ConvertedThing]> that emits one value when all the conversions have completed. How can this be accomplished? Any help much appreciated! 回答1: You can use .merge() to combine the array of observables into a single observable, and then use .toArray() to get them as a list in a single event. For RxSwift 3+ use

RxSwift and isSelected property on UIButton

梦想与她 提交于 2019-11-30 05:47:53
I have three buttons and I want them to be selected only one at a time: and: etc... My approach is this: class MyController: UIViewController { @IBOutlet var buttonOne: UIButton! @IBOutlet var buttonTwo: UIButton! @IBOutlet var buttonThree: UIButton! var buttonOneIsSelected = Variable(true) var buttonTwoIsSelected = Variable(false) var buttonThreeIsSelected = Variable(false) override func viewDidLoad() { super.viewDidLoad() buttonOne.isSelected = true buttonOneIsSelected.asDriver() .drive(buttonOne.rx.isSelected) .disposed(by: disposeBag) buttonTwoIsSelected.asDriver() .drive(buttonTwo.rx

How do I create an observable of an array from an array of observables?

别等时光非礼了梦想. 提交于 2019-11-30 01:58:18
I have an array of Thing objects that I want to convert to ConvertedThing objects, using an asynchronous function that returns Observable<ConvertedThing> . I'd like to create an Observable<[ConvertedThing]> that emits one value when all the conversions have completed. How can this be accomplished? Any help much appreciated! kennytm You can use .merge() to combine the array of observables into a single observable, and then use .toArray() to get them as a list in a single event. For RxSwift 3+ use: let arrayOfObservables: [Observable<E>] = ... let singleObservable: Observable<E> = Observable

RxSwift - Debounce/Throttle “inverse”

a 夏天 提交于 2019-11-30 00:29:27
问题 Let's say I have an instant messaging app that plays a beep sound every time a message arrives. I want to debounce the beeps, but I'd like to play the beep sound for the first message arrived and not for the following ones (in a timespan of, say, 2 seconds). Another example might be: my app sends typing notifications (so the user I'm chatting with can see that I'm typing a message). I want to send a typing notification when I start typing, but only send new ones in X-seconds intervals, so I

Two way binding in RxSwift

删除回忆录丶 提交于 2019-11-29 18:37:35
问题 I read the two way binding operator in sample code of RxSwift. func <-> <T>(property: ControlProperty<T>, variable: Variable<T>) -> Disposable { let bindToUIDisposable = variable.asObservable() .bindTo(property) let bindToVariable = property .subscribe(onNext: { n in variable.value = n }, onCompleted: { bindToUIDisposable.dispose() }) return StableCompositeDisposable.create(bindToUIDisposable, bindToVariable) } When property changed, it will notify variable, and set the variable's value,

Binding to a UIRefreshControl after network call

耗尽温柔 提交于 2019-11-29 15:49:44
问题 I am new to RxSwift and I was wondering how I would be able to "reactively" use a UIRefreshControl with a UITableView instead of the normal way of creating a target, and manually calling beginRefreshing() and endRefreshing() . For instance, say I am loading some strings from an API: class TableViewController: UITableViewController { var data : [String] = [] let db = DisposeBag() override func viewDidLoad() { super.viewDidLoad() refreshControl = UIRefreshControl() //I don't want to use /

Using retryWhen to update tokens based on http error code

落爺英雄遲暮 提交于 2019-11-29 11:31:44
问题 I found this example on How to refresh oauth token using moya and rxswift which I had to alter slightly to get to compile. This code works 80% for my scenario. The problem with it is that it will run for all http errors, and not just 401 errors. What I want is to have all my other http errors passed on as errors, so that I can handle them else where and not swallow them here. With this code, if I get a HttpStatus 500 , it will run the authentication code 3 times which is obviously not what I