RxSwift (Tips)

refering to:https://github.com/ReactiveX/RxSwift/blob/master/Documentation/Tips.md
Always strive to model your systems or their parts as pure functions.Those pure functions can be tested easily and can be used to modify operator behaviors.
When you are using Rx,first try to compose built-in operator.
If using some combination of operators often, create your convenience operators.
extension ObservableType where E: MaybeCool {
@warn_unused_result(message=”http://git.io/rxs.uo”)
public func coolEelements()
-> Observable {
return filter{ e -> Bool in
return e.isCool
}
}
}
Rx operators are as general as possible, but there will always be edge cases that will be hard to model.In those cases you can just create your own operator and possibly use one of the built-in operators as reference.
Always use operators to compose subscriptions.
Avoid nesting subscribe calls at all cost.This is a code smell.
textField.rx.text.subscribe(onNext: { text in
performURLRequest(text).subscribe(onNext: { result in
….
})
.addDisposable(disposeBag)
})
.addDisposableTo(disposeBag)

Preferred way of changing disposables by using operators.
textField.rx.text
.flatMapLatest { text in
return performURLRequest(text)
}

.addDisposableTo(disposeBag)//only one top most disposable

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值