rx-swift

RxSwift : BehaviorRelay in place of Variable usage

旧街凉风 提交于 2019-12-22 10:57:01
问题 I'm new to RxSwift and reading about subjects, I tried Variable Subject. Which in turns giving Warning in console ℹ️ [DEPRECATED] `Variable` is planned for future deprecation. Please consider `BehaviorRelay` as a replacement. Read more at: https://git.io/vNqvx Earlier I have declared Variable like this var searchItems = Variable<[MyClass]>([]) So i have done basic array operations from it's property called value as it was get set property like 1. self.searchItems.value.removeAll() 2. self

How to select CollectionView cell in RxSwift

怎甘沉沦 提交于 2019-12-22 04:13:07
问题 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? 回答1: 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

RxSwift: Observable while a button holds down

流过昼夜 提交于 2019-12-21 06:04:22
问题 How to create Observable which streams an event repeatedly while a button holds down? 回答1: 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

'[weak self]' in RXSwift closures

巧了我就是萌 提交于 2019-12-20 09:50:40
问题 Do i need to use [weak self] within RXSwift subscribeNext closures? I have the code: searchController.searchBar.rx_text.throttle(0.2, scheduler: MainScheduler.instance).subscribeNext { searchText in self.viewModel.searchForLocation(searchText) }.addDisposableTo(DisposelBag.sharedDisposelBag.disposeBag) Do i need to modify it so that there is a [weak self] capture list at the beginning of the closure? Like this: searchController.searchBar.rx_text.throttle(0.2, scheduler: MainScheduler.instance

Does the order of subscribeOn and observeOn matter?

天涯浪子 提交于 2019-12-20 08:57:40
问题 I'm a little bit confused about the order you can call the subscribeOn and observeOn methods on observables. I read a couple of posts and one guys says that it doesn't matter and just uses thing in his example and other people say it does matter. So here is my question: For example: self.remoteService.rxGetAllLanguages() .observeOn(MainScheduler.instance) .subscribeOn(ConcurrentDispatchQueueScheduler(globalConcurrentQueueQOS: .Background)) .subscribe({ e in switch e { case .Next(let element):

Cannot receive event with custom DelegateProxy and Protocol

让人想犯罪 __ 提交于 2019-12-20 06:24:54
问题 I try to migrate delegate of DifficultyViewDelegate to observable. This is my DifficultyViewDelegate : @objc protocol DifficultyViewDelegate: class { func levelDidIncrease() func levelDidDecrease() } And my DifficultyView : weak var delegate: DifficultyViewDelegate? @IBAction func decreaseLevel(_ sender: Any) { delegate?.levelDidDecrease() } @IBAction func increaseLevel(_ sender: Any) { delegate?.levelDidIncrease() } And this is my RxDifficultyViewDelegateProxy class

Using 'self' on RxSwift closures… What about instance methods as param?

白昼怎懂夜的黑 提交于 2019-12-20 03:24:15
问题 In other stack overflow questions, it was emphasized that the capture [weak self] should be used for closures that aren't owned by the class because self could be nil before the closure completes. An alternative when the closure is owned by the class itself is [unowned self] . My question is do I need to use [unowned self] when the function I pass as a parameter is an instance method of the current class? Example import RxSwift class Person { var name = "Default name" class func getPersons()

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

别说谁变了你拦得住时间么 提交于 2019-12-18 19:42:28
问题 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? 回答1: 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

How to pass data from delegate method to the observable's onNext method in RxSwift?

放肆的年华 提交于 2019-12-18 09:53:30
问题 I have manager class which will connect and manage the data and state of the Bluetooth device. The manager class conforms to IWDeviceManagerDelegate and has a method which gives the weight data func onReceiveWeightData(_ device: IWDevice!, data: IWWeightData!) . Once I call listenToWeight() from any controller I want to give the data using Observable. How I fire an onNext event with the data of onReceiveWeightData method to listenToWeight observable? Below is the code. class

RxSwift reload tableview

核能气质少年 提交于 2019-12-18 09:35:14
问题 let officialAccountObservable : Observable<[SearchUser]> = SearchAPI.sharedAPI.suggestAccounts() officialAccountObservable.bind(to: tableView.rx.items(cellIdentifier: "followcell", cellType: FollowCell.self)) { (index, user , cell) in if user.profileImagePath.isEmpty == false { cell.profile.af_setImage(withURL: URL.init(string: user.profileImagePath)!) }else { cell.profile.image = UIImage.init(named: "icon_user_03") } cell.nickName.text = user.nickName cell.follow.rx.tap .debounce(0.3,