RxJava:如何从不同的函数中提取相同的 observeOn?
RxJava: How to extract same observeOn from different functions?
基本上我的 Android 应用程序有一些元数据需要在不同情况下报告给后端服务器:
data class SearchMetaData(
val searchId: String?,
val isRateLimit: Boolean
)
为了代码简洁,我把最小的case做了如下。所有的上报逻辑都差不多,每个函数subscribe
元数据提供者和获取需要上报的值。
fun logEvent1() {
fetchMetaData().observeOn(schedulers.mainThread()).subscribe({ metadata ->
...//lots of other events data here
val sessionMetadata = SessionMetadata()
sessionMetadata.id = metadata.searchId
sessionMetadata.limiit = metadata.isRateLimit
event1.session = sessionMetadata
...
report(event1)
})
}
fun logEvent2() {
fetchMetaData().observeOn(schedulers.mainThread()).subscribe({ metadata ->
...//lots of other events data here
val sessionMetadata = SessionMetadata()
sessionMetadata.id = metadata.searchId
sessionMetadata.limiit = metadata.isRateLimit
event2.session = sessionMetadata
...
report(event2)
})
}
fun logEvent3() {
fetchMetaData().observeOn(schedulers.mainThread()).subscribe({ metadata ->
...//lots of other events data here
val sessionMetadata = SessionMetadata()
sessionMetadata.id = metadata.searchId
sessionMetadata.limiit = metadata.isRateLimit
event3.session = sessionMetadata
...
report(event3)
})
}
我担心的是每次我们更改元数据架构时,我们都需要更新所有 logEventX
,我想知道是否可以在不同的函数中提取所有 subscribe
并获得元数据?
考虑使用 compose 和 doOnSuccess 的扩展函数
Single<MetaData>.handleLogging() : Single<MetaData>{
return compose{
it.doOnSuccess{ metaData ->
val sessionMetadata = SessionMetadata()
sessionMetadata.id = metadata.searchId
sessionMetadata.limiit = metadata.isRateLimit
report(sessionMetaData)
}
}
}
//usage
fetchMetaData().handleLogging().subscribe{
//other uncommon logic here.
}
基本上我的 Android 应用程序有一些元数据需要在不同情况下报告给后端服务器:
data class SearchMetaData(
val searchId: String?,
val isRateLimit: Boolean
)
为了代码简洁,我把最小的case做了如下。所有的上报逻辑都差不多,每个函数subscribe
元数据提供者和获取需要上报的值。
fun logEvent1() {
fetchMetaData().observeOn(schedulers.mainThread()).subscribe({ metadata ->
...//lots of other events data here
val sessionMetadata = SessionMetadata()
sessionMetadata.id = metadata.searchId
sessionMetadata.limiit = metadata.isRateLimit
event1.session = sessionMetadata
...
report(event1)
})
}
fun logEvent2() {
fetchMetaData().observeOn(schedulers.mainThread()).subscribe({ metadata ->
...//lots of other events data here
val sessionMetadata = SessionMetadata()
sessionMetadata.id = metadata.searchId
sessionMetadata.limiit = metadata.isRateLimit
event2.session = sessionMetadata
...
report(event2)
})
}
fun logEvent3() {
fetchMetaData().observeOn(schedulers.mainThread()).subscribe({ metadata ->
...//lots of other events data here
val sessionMetadata = SessionMetadata()
sessionMetadata.id = metadata.searchId
sessionMetadata.limiit = metadata.isRateLimit
event3.session = sessionMetadata
...
report(event3)
})
}
我担心的是每次我们更改元数据架构时,我们都需要更新所有 logEventX
,我想知道是否可以在不同的函数中提取所有 subscribe
并获得元数据?
考虑使用 compose 和 doOnSuccess 的扩展函数
Single<MetaData>.handleLogging() : Single<MetaData>{
return compose{
it.doOnSuccess{ metaData ->
val sessionMetadata = SessionMetadata()
sessionMetadata.id = metadata.searchId
sessionMetadata.limiit = metadata.isRateLimit
report(sessionMetaData)
}
}
}
//usage
fetchMetaData().handleLogging().subscribe{
//other uncommon logic here.
}