rx-swift

How do I implement a Swift protocol with a generic constrained type property?

只愿长相守 提交于 2019-12-05 17:33:35
I would like to have a protocol that looks something like this: protocol ReturnType { var returnType: ImmutableMappable.Type { get } } The part of the enum implementing the protocol: extension ShimEndPoint: ReturnType { var returnType: ImmutableMappable.Type { switch self { case .deAuthorize(_, _): return EmptyResponse.self case .authorize(_, _): return AuthorizeResponse.self case .step(_, _, _, _): return StepResponse.self } } } EmptyResponse, AuthorizeResponse and StepResponse all implement ImmutableMappable. Now I would like to use the "returnType" property in a function call: return Shim

RxSwift: Two way binding

给你一囗甜甜゛ 提交于 2019-12-05 06:48:29
问题 I used official two-way-binding solution 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 Disposables.create(bindToUIDisposable, bindToVariable) } Usage: (textField.rx.text <-> object.property).addDisposableTo(disposeBag) Property definition: var property = Variable

Subscription to a UIButton.rx.tap located in UITableViewCell within UITableViewDataSource

夙愿已清 提交于 2019-12-05 05:02:42
问题 Let's say I have a UIButton in a UITableViewCell . After dequeuing the cell from the UITableView I want to subscribe to the UIButton.rx.tap . The issue is that if my UITableViewCell is dequeued multiple times, the subscriptions would retain. Currently I solve this problem by allocating a Disposable property in my UITableViewCell , setting it when the subscription is create, and calling Disposable.dispose() on UITableViewCell.prepareForReuse() , however as far as I understand implementing

How to select CollectionView cell in RxSwift

南楼画角 提交于 2019-12-05 03:22:17
I need to select the item at specific index in collection view using RxSwift.This method is not working fine. collectionView.rx.modelSelected(SearchResult.self).subscribe(onNext:{ menuItem in }).addDisposableTo(disposeBag) Can anybody help? If you want the indexPath of item selected you can use the following : collectionView .rx .itemSelected .subscribe(onNext:{ indexPath in //your code }).disposed(by: disposeBag) and if you want to the model being selected : collectionView .rx .modelSelected(SearchResult.self) .subscribe(onNext: { (model) in //Your code }).disposed(by: disposeBag) And you can

Reload Tableview using RxSwift

試著忘記壹切 提交于 2019-12-05 02:34:42
问题 I am using RxSwift for tableview. I need to reload my table each time after getting data from api but I'm failed to do this. I couldn't find any solution for that. Can anybody help? I have an array of places obtain from response of an Api.I have used this code in view did load, but its is not being called when array is updated. 回答1: I have found the issue. My array was not being getting updated correctly. I did the following changes. Declare dataSource variable of ModelClass: let dataSource =

RxSwift replacement shouldChangeCharactersInRange

…衆ロ難τιáo~ 提交于 2019-12-04 13:20:33
问题 I want use UITextfield with RxSwift. My goal is allowing/not input character in User keyboard and removing character from copy paste, I need handle UITextfield's delegate "shouldChangeCharactersInRange" with RxSwift . How to implement with RxSwift ? I am using RxSwift version 4. Case 1: Input from keyboard: A123 Process from RxSwift : Accept 123 (not allowing NumberPad) Output : 123 Case 2: Input form Copy Paste from Contacts: \U202d1111111111\U202c Process from RxSwift : remove all control

Handling Network error in combination with binding to tableView (Moya, RxSwift, RxCocoa)

流过昼夜 提交于 2019-12-04 10:51:11
问题 I am currently using Moya to make my network requests. I have implemented the following from one of the example projects @ https://github.com/DroidsOnRoids/RxSwiftExamples#tutorials Below I have set up restaurantSearch so that when someone enters text it makes a new request. var restaurantSearch: Observable<(String)> { return searchBar .rx_text .throttle(0.5, scheduler: MainScheduler.instance) .distinctUntilChanged() } I have a method that returns an observable of type [Restaurant] func

Bind two UIview fram/Position using Rxswift

一个人想着一个人 提交于 2019-12-04 08:26:24
I want to change view2 position automatically when view1 position will change and bind the both view position using Rxswift. I try to observe view frame/position using this view.rx.observe(CGRect.self,"frame") .subscribe(onNext: { print($0 ?? (0,0)) }) it print frame on init time but when change view position using constraints self.constraintHorizontalCenterView.constant = 1000 it print nothing means this code not observe view position... Is there any way to observe continuously view position or bind view position? from apple ' frame ' // animatable. do not use frame if view is transformed

Limiting concurrent access to a service class with RxSwift

南楼画角 提交于 2019-12-04 05:12:09
问题 Given a service class like this: class Service { let networkService = NetworkService() func handleJobA(input: String) -> Observable<ResultA> { return networkService .computeA(input) .map { $0.a } } } And when I use it from the caller side like this: let service = Service() Observable .from(["Hello", "World"]) .flatMap { service.handleJobA($0) } .subscribe() Then this would send multiple requests to service at the same time. I wanted for the stream to wait until each request is done. That was

RxSwift: Observable while a button holds down

非 Y 不嫁゛ 提交于 2019-12-03 21:17:21
How to create Observable which streams an event repeatedly while a button holds down? Even I was looking for a solution for your question. I got help from RxSwift slack channel. let button = submitButton.rx_controlEvent([.TouchDown]) button .flatMapLatest { _ in Observable<Int64>.interval(0.1, scheduler: MainScheduler.instance) .takeUntil(self.submitButton.rx_controlEvent([.TouchUpInside])) } .subscribeNext{ x in print("BOOM \(x)") } .addDisposableTo(disposeBag) //prints BOOM 0 BOOM 1 BOOM 2 BOOM 3 BOOM 4 BOOM 5 for every 0.1 seconds And also Check Interval Documentation .Thanks to @jari of