我有一些测试函数,观察者将其值化并将其映射到另一个Observable
:
private func test1() {
selectedTagsVariable
.asObservable()
.flatMapLatest { [weak self] tags -> Observable<PostSet> in
guard let strongSelf = self else { return .empty() }
return strongSelf.postSetObservable(for: tags)
}
}
没关系,但我不希望每次检查自我值为零。当然,我可以将weak
替换为unowned
,但这不正确。
所以,我创建了自定义flatMapLatest
:
extension ObservableType {
func xflatMapLatest<A:AnyObject, O: ObservableType>(weak obj: A, selector: @escaping (A, Self.E) throws -> O) -> Observable<O.E> {
return flatMapLatest { [weak obj] value -> Observable<O.E> in
guard let strongObj = obj else {
return Observable.empty()
}
return try selector(strongObj, value) as! Observable<O.E>
}
}
}
它看起来像这样:
private func test2() {
selectedTagsVariable
.asObservable()
.xflatMapLatest(weak: self) { obj, tags -> Observable<PostSet> in
return obj.postSetObservable(for: tags)
}
}
问题:我不喜欢xflatMapLatest
函数中的返回类型(as! Observable<O.E>
)中的类型转换。我可以以某种方式删除它吗?帮助重构这个方法:)
答案 0 :(得分:2)
您想要的是通过简单的修改来镜像flatMapLatest
运算符。看看它的签名:
func flatMapLatest<O : ObservableConvertibleType>(_ selector: @escaping (Self.E) throws -> O) -> RxSwift.Observable<O.E>
如您所见,selector
闭包的结果类型必须符合ObservableConvertibleType
,其定义在此处:
public protocol ObservableConvertibleType {
associatedtype E
public func asObservable() -> RxSwift.Observable<Self.E>
}
了解这一点很容易看出你需要做些什么改变才能让它发挥作用:
extension ObservableType {
func flatMapLatestWeak<A: AnyObject, O: ObservableConvertibleType>(weak obj: A, selector: @escaping (A, E) throws -> O) -> Observable<O.E> {
return flatMapLatest { [weak obj] value -> Observable<O.E> in
guard let strongObj = obj else {
return Observable.empty()
}
return try selector(strongObj, value).asObservable()
}
}
}
此外,您可以使用Optional<T>
map
缩短此方法:
extension ObservableType {
func flatMapLatestWeak<A: AnyObject, O: ObservableConvertibleType>(weak obj: A, selector: @escaping (A, E) throws -> O) -> Observable<O.E> {
return flatMapLatest { [weak obj] value in
try obj.map{ try selector($0 , value).asObservable() } ?? .empty()
}
}
}