rxjava-to-coroutines-migration
Guide and execute the migration of asynchronous code from RxJava to Kotlin Coroutines and Flow. Use this skill when a user asks to convert RxJava (O…
它会碰到什么
扫了多少1 个文本文件,5 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0
这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。
技能内容
RxJava to Kotlin Coroutines Migration Skill
A specialized skill designed to safely and idiomatically refactor Android or Kotlin codebases from RxJava to Kotlin Coroutines and Flow.
Migration Mapping Guide
When migrating RxJava components to Kotlin Coroutines, use the following standard mappings:
1. Base Types
Single<T>->suspend fun ...(): T- A single asynchronous value.
Maybe<T>->suspend fun ...(): T?- A single asynchronous value that might not exist.
Completable->suspend fun ...()- An asynchronous operation that completes without a value.
Observable<T>->Flow<T>- A cold stream of values.
Flowable<T>->Flow<T>- Coroutines Flow natively handles backpressure.
2. Subjects to Hot Flows
PublishSubject<T>->MutableSharedFlow<T>- Broadcasts events to multiple subscribers. Use
MutableSharedFlow(extraBufferCapacity = ...)if buffering is needed. BehaviorSubject<T>->MutableStateFlow<T>- Holds state and emits the current/latest value to new subscribers. Requires an initial value.
ReplaySubject<T>->MutableSharedFlow<T>(replay = N)- Replays the last N emitted values to new subscribers.
3. Schedulers to Dispatchers
Schedulers.io()->Dispatchers.IOSchedulers.computation()->Dispatchers.DefaultAndroidSchedulers.mainThread()->Dispatchers.Main- Context Switching:
subscribeOnandobserveOnare typically replaced bywithContext(Dispatcher)orflowOn(Dispatcher)for Flows.
4. Operators
map->mapfilter->filterflatMap->flatMapMerge(concurrent) orflatMapConcat(sequential)switchMap->flatMapLatestdoOnNext/doOnSuccess->onEachonErrorReturn/onErrorResumeNext->catch { emit(...) }startWith->onStart { emit(...) }combineLatest->combinezip->zipdelay->delay(suspend function) oronEach { delay(...) }
5. Execution and Lifecycle
subscribe()->collect {}(for Flows) or direct invocation (for suspend functions) inside aCoroutineScope.Disposable.dispose()->Job.cancel()CompositeDisposable.clear()-> Cancel the parentCoroutineScopeorJob.
Execution Steps
- Analyze the RxJava Chain: Identify the source type (Single, Observable, etc.), operators used, and where the subscription happens.
- Convert the Source: Change the return type in the repository or data source layer first. Convert to
suspendfunctions for one-shot operations, andFlowfor streams. - Rewrite Operators: Translate the RxJava operators to their Flow or Coroutine equivalents. Note that many RxJava operators can simply be replaced by standard Kotlin collection/sequence operations inside a
maporonEachblock. - Update the Subscription: Replace
.subscribe(...)withlaunch { ... }and.collect { ... }in the ViewModel or Presenter. Ensure the launch is tied to the correct lifecycle scope (e.g.,viewModelScope). - Handle Errors: Replace
onErrorblocks withtry/catcharound suspend functions, or.catch { }operators on Flows. - Handle Threading: Remove
.subscribeOn()and.observeOn(). UsewithContextwhere necessary, or.flowOn()to change the context of the upstream flow.
Example Transformation
RxJava:
fun getUser(id: String): Single<User> { ... }
disposable.add(
getUser("123")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ user ->
view.showUser(user)
}, { error ->
view.showError(error)
})
)
Coroutines/Flow:
suspend fun getUser(id: String): User { ... } // Internally uses withContext(Dispatchers.IO) if needed
viewModelScope.launch {
try {
val user = getUser("123")
view.showUser(user)
} catch (e: Exception) {
view.showError(e)
}
}
Best Practices
- Favor Suspend Functions: Default to
suspendfunctions instead ofFlowunless you actually have a stream of multiple values over time.SingleandCompletablealmost always becomesuspendfunctions. - State Handling: Use
StateFlowin ViewModels to expose state to the UI instead ofBehaviorSubjectorLiveData. - Lifecycle Awareness: Use
repeatOnLifecycleorflowWithLifecyclein the UI layer when collecting Flows to avoid background work when the view is not visible.
想直接用这个技能?
本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。
它属于哪个仓库
星标★ 963
本站分层T2
该仓技能数17
原文件路径
.github/skills/migration/rxjava-to-coroutines-migration/SKILL.md