rx-swift

Does the order of subscribeOn and observeOn matter?

无人久伴 提交于 2019-12-02 17:33:47
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): case .Error(let e): DDLogError("Error in \(e)") case .Completed: DDLogDebug("Completed") } } )

setting ids on item in Stackview

ぃ、小莉子 提交于 2019-12-02 15:58:12
问题 I have an array of object which I used to create a checkbox. The model has an id, name. I created a stackView to handle checkbox with id now I want to append items of selected checkbox to an array and be able to remove them when deselected. I am able to present all the views and it works well below is my code NetworkAdapter.instance.getFeaturesAmeneities() .subscribe(onNext: {feat in guard let data = feat.data else {return} self.features.append(contentsOf: data) self.stackFeature.axis =

Swift Struct Memory Leak

ⅰ亾dé卋堺 提交于 2019-12-02 15:51:50
We're trying to use Swift structs where we can. We are also using RxSwift which has methods which take closures. When we have a struct that creates a closure that refers to self , that creates a strong reference cycle . import Foundation import RxSwift struct DoesItLeak { var someState: String = "initial value" var someVariable: Variable<String> = Variable("some stuff") let bag = DisposeBag() mutating func someFoo() { someVariable.subscribeNext { person in self.someState = "something" } .addDisposableTo(bag) } } How do I know this? If I create 100,000 DoesItLeak objects and call someFoo() on

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

风格不统一 提交于 2019-12-02 14:28:29
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 declaration var myFilter = Variable<[MyFilterModel]>([MyFilterModel(data: "{:}")]) on getting a new

RxSwift - Generic parameter 'Self' could not be inferred

ⅰ亾dé卋堺 提交于 2019-12-02 12:38:43
问题 I have an UITableView and a countries variable whose signature is like: let countryArray = ["Bangladesh", "India", "Pakistan", "Nepal", "Bhutan", "China", "Malaysia", "Myanmar", "Sri Lanka", "Saudi Arabia"] When I'm trying to bind this array of countries in a UITableView, Its showing the error Generic parameter 'Self' could not be inferred . Here is the snippet that I am doing: let countries = Observable.just(countryArray) countries.bindTo(self.tableView.rx.items(cellIdentifier: "myCell",

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

元气小坏坏 提交于 2019-12-02 12:29:45
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 WeightMachineManager: NSObject { func setup() { IWDeviceManager.shared()?.delegate = self IWDeviceManager.shared()?

setting ids on item in Stackview

*爱你&永不变心* 提交于 2019-12-02 11:51:33
I have an array of object which I used to create a checkbox. The model has an id, name. I created a stackView to handle checkbox with id now I want to append items of selected checkbox to an array and be able to remove them when deselected. I am able to present all the views and it works well below is my code NetworkAdapter.instance.getFeaturesAmeneities() .subscribe(onNext: {feat in guard let data = feat.data else {return} self.features.append(contentsOf: data) self.stackFeature.axis = .vertical self.stackFeature.distribution = .fill self.stackFeature.spacing = 8 data.forEach { print($0.id)

Simple observable struct with RxSwift?

半世苍凉 提交于 2019-12-01 12:47:14
I'm trying to come up with a simple observable object in Swift and thought to use RxSwift . I couldn't find a simple example to do something like this: protocol PropertyObservable { typealias PropertyType var propertyChanged: Event<(PropertyType, Any)> { get } } class Car: PropertyObservable { typealias PropertyType = CarProperty let propertyChanged = Event<(CarProperty, Any)>() dynamic var miles: Int = 0 { didSet { propertyChanged.raise(.Miles, oldValue as Any) } } dynamic var name: String = "Turbo" { didSet { propertyChanged.raise(.Name, oldValue as Any) } } } The above is pure Swift

RxSwift - how to chain observables sequentially

回眸只為那壹抹淺笑 提交于 2019-12-01 10:29:26
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: argument) ?? Observable.empty()) } let result = Observable.concat(observables) This approach however

Simple observable struct with RxSwift?

空扰寡人 提交于 2019-12-01 10:09:50
问题 I'm trying to come up with a simple observable object in Swift and thought to use RxSwift. I couldn't find a simple example to do something like this: protocol PropertyObservable { typealias PropertyType var propertyChanged: Event<(PropertyType, Any)> { get } } class Car: PropertyObservable { typealias PropertyType = CarProperty let propertyChanged = Event<(CarProperty, Any)>() dynamic var miles: Int = 0 { didSet { propertyChanged.raise(.Miles, oldValue as Any) } } dynamic var name: String =