rx-swift

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

邮差的信 提交于 2019-12-03 20:55:09
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 features in a way that requires you to call Disposable.dispose() implies that you are doing something wrong

RxSwift: Two way binding

偶尔善良 提交于 2019-12-03 20:34:31
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<String?>(nil) In onNext method all ok and variable changed its value, but my object.property doesn't

Reload Tableview using RxSwift

☆樱花仙子☆ 提交于 2019-12-03 20:24:34
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. Mehreen 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 = Variable<[SearchResult]>([]) Bind it with the table view right now it is empty: dataSource

Proper way to dispose a one-off observable in RxSwift

你。 提交于 2019-12-03 15:16:24
问题 I have an observable that I only want to kick off once. The docs say: Using dispose bags or takeUntil operator is a robust way of making sure resources are cleaned up. We recommend using them in production even if the sequences will terminate in finite time. My observable terminates after just one event let observable = Observable.create() { observer in webservice.makeHTTPRequestWithCompletionBlock { if something { observer.on(.Next(...)) observer.onCompleted() } else { observer.on(.Error(...

How to observe array property changes in RxSwift

▼魔方 西西 提交于 2019-12-03 13:39:08
Here is my class: class ViewController: UIViewController { var myArray : NSArray! } I want to fire an event every time myArray points to a new array, like this: self.myArray = ["a"] self.myArray = ["b"] I've tried rx_observe but failed, here is my code: self.rx_observe(NSArray.self, "myArray").subscribeNext { (array) -> Void in print(array) } It only fires the first time, what's the problem? Most of the time, if you have control of the backing variable, you would prefer Variable to using rx_observe . class ViewController: UIViewController { var myArray : Variable<NSArray>! } The first time you

Proper usage of RxSwift to chain requests, flatMap or something else?

笑着哭i 提交于 2019-12-03 07:30:33
First of all, I'm new to rxswift so I guess the answer is obvious however at the moment I can't find solution by myself. I have two functions: func downloadAllTasks() -> Observable<[Task]> func getTaskDetails(taskId: Int64) -> Observable<TaskDetails> First one is downloading the list of Task objects using network request, second one downloading task details for sepcific task (using it's id) What I want of achieve is to download all tasks and then for each task I want to download its details and subscribe for the event fired when all tasks details are ready. So I guess I should subscribe

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

耗尽温柔 提交于 2019-12-03 06:46:05
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 restaurants() -> Observable<[Restaurant]> { return restaurantSearch .observeOn(MainScheduler.instance)

How to bind rx_tap (UIButton) to ViewModel?

旧时模样 提交于 2019-12-03 06:39:22
I have authorization controller with 2 UITextField properties and 1 UIButton. I want to bind my View to ViewModel but don't know how to to do it. This is my AuthorizatioVC.swift: class AuthorizationViewController: UIViewController { let disposeBag = DisposeBag() @IBOutlet weak var passwordTxtField: UITextField! @IBOutlet weak var loginTxtField: UITextField! @IBOutlet weak var button: UIButton! override func viewDidLoad() { super.viewDidLoad() addBindsToViewModel() } func addBindsToViewModel(){ let authModel = AuthorizationViewModel(authClient: AuthClient()) authModel.login.asObservable()

Observing UITextField.editing with RxSwift

↘锁芯ラ 提交于 2019-12-03 05:58:21
I want to observe the property UITextfield.editing . I'm using this code: self.money.rx_observe(Bool.self, "editing").subscribeNext { (value) in print("") }.addDisposableTo(disposeBag) But in the process of running, it's only performed once. How do I solve this,please solidcell Don't observe the editing property, because it's not just a stored property. It's defined as: public var editing: Bool { get } So you don't know how UIKit is actually getting that value. Instead, use rx.controlEvent and specify the control events you're interested in, like so: textField.rx.controlEvent([.editingDidBegin

How to use BehaviorRelay as an alternate to Variable in RxSwift?

╄→尐↘猪︶ㄣ 提交于 2019-12-03 01:10:18
问题 As of RxSwift4, Variable is moved to Deprecated.swift marking the possible deprecation of Variable in future. An alternate proposed to Variable is BehaviorRelay . While posting this question, as I could not find much of the tutorial on web using BehaviorRelay am posting such a fundamental question here in SO. Assume I have a webService call going on and I receive a chunk of data which is JSONArray, on parsing JSON object one by one I update my Variable's value property Here is my variable