5.1 Jetpack组件深度集成
5.1.1 Lifecycle协程支持
源码解析:LifecycleCoroutineScope
kotlin
// LifecycleController.kt 核心实现
internal class LifecycleController(
private val lifecycle: Lifecycle,
minState: Lifecycle.State,
private val dispatchQueue: DispatchQueue
) {
private val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_DESTROY) {
// 1. 生命周期销毁时取消协程
handleDestroy()
} else if (event.targetState == Lifecycle.State.DESTROYED) {
// 2. 安全移除观察者
lifecycle.removeObserver(this)
}
}
init {
if (lifecycle.currentState == Lifecycle.State.DESTROYED) {
// 3. 立即取消协程
handleDestroy()
} else {
// 4. 注册生命周期观察者
lifecycle.addObserver(observer)
}
}
private fun handleDestroy() {
dispatchQueue.cancel() // 5. 取消队列中所有协程
lifecycle.removeObserver(observer) // 6. 移除观察者
}
}
集成最佳实践:
kotlin
// Fragment中安全启动协程
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// 自动绑定Fragment生命周期
viewLifecycleOwner.lifecycleScope.launch {
// 当Fragment进入STARTED状态时执行
whenStarted {
loadData()
}
}
}
// 精确控制协程生命周期
viewLifecycleOwner.lifecycleScope.launchWhenResumed {
// 仅在Resumed状态执行
animateView()
}
5.1.2 Room协程支持
DAO方法实现原理:
kotlin
// CoroutinesRoom.kt#execute() 核心方法
suspend fun <R> execute(block: suspend () -> R): R {
// 1. 检查协程是否已取消
if (isSuspendingTransactionActive()) {
return block()
}
// 2. 创建协程事务
return withContext(transactionDispatcher) {
// 3. 开始事务
beginTransaction()
try {
// 4. 执行数据库操作
val result = block()
// 5. 提交事务
setTransactionSuccessful()
result
} finally {
// 6. 结束事务
endTransaction()
}
}
}
Flow查询优化:
kotlin
@Dao
interface UserDao {
// 自动生成Flow实现
@Query("SELECT * FROM users")
fun getUsersFlow(): Flow<List<User>>
// 自定义Flow查询
@Query("SELECT * FROM users WHERE age > :minAge")
fun getAdultUsers(minAge: Int): Flow<List<User>>
}
// ViewModel中使用
val users: Flow<List<User>> = userDao.getUsersFlow()
.map { users ->
// 在主线程外处理转换
users.filter { it.isActive }
}
.flowOn(Dispatchers.Default) // 指定上游线程
5.2 UI编程最佳实践
5.2.1 主线程安全访问
安全更新UI模式:
kotlin
// 错误方式:潜在线程安全问题
fun updateUI(data: Data) {
// 可能在后台线程调用
textView.text = data.title
imageView.setImageBitmap(data.image)
}
// 正确方式1:确保主线程
suspend fun safeUpdateUI(data: Data) {
withContext(Dispatchers.Main) {
textView.text = data.title
imageView.setImageBitmap(data.image)
}
}
// 正确方式2:使用Flow自动切换
viewModel.uiState
.flowOn(Dispatchers.Main) // 确保下游在主线程
.onEach { state ->
when (state) {
is Loading -> showProgress()
is Success -> renderData(state.data)
is Error -> showError(state.exception)
}
}
.launchIn(lifecycleScope)
5.2.2 UI事件处理优化
防抖与节流实现:
scss
// 搜索框输入防抖
val searchQuery = MutableStateFlow("")
init {
viewModelScope.launch {
searchQuery
.debounce(300) // 300ms防抖
.distinctUntilChanged() // 值相同时跳过
.filter { it.length > 2 } // 最小长度限制
.flatMapLatest { query ->
// 取消前一个搜索请求
repository.search(query)
.catch { emit(emptyList()) } // 错误处理
}
.collect { results ->
// 更新UI
updateSearchResults(results)
}
}
}
// 按钮点击防抖
fun onButtonClick() {
viewModelScope.launch {
if (!isProcessing) {
isProcessing = true
performAction()
delay(500) // 最小点击间隔
isProcessing = false
}
}
}
5.3 WorkManager协程支持
5.3.1 CoroutineWorker深度集成
kotlin
class UploadWorker(
context: Context,
params: WorkerParameters
) : CoroutineWorker(context, params) {
override suspend fun doWork(): Result {
// 1. 获取输入数据
val fileUri = inputData.getString("file_uri") ?: return Result.failure()
return try {
// 2. 执行上传操作
val result = uploadFile(fileUri)
// 3. 返回结果
if (result.success) {
Result.success()
} else {
Result.retry() // 可配置重试
}
} catch (e: Exception) {
// 4. 错误处理
if (runAttemptCount < 3) {
Result.retry()
} else {
Result.failure()
}
}
}
private suspend fun uploadFile(uri: String): UploadResult {
// 使用协程执行网络请求
return withContext(Dispatchers.IO) {
// 实际上传逻辑
}
}
}
// 启动工作
val uploadWork = OneTimeWorkRequestBuilder<UploadWorker>()
.setInputData(workDataOf("file_uri" to fileUri.toString()))
.setConstraints(
Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
)
.build()
WorkManager.getInstance(context).enqueue(uploadWork)
5.4 资源安全访问策略
5.4.1 文件操作安全模式
scss
suspend fun writeToFile(data: ByteArray, file: File) {
// 使用IO调度器
withContext(Dispatchers.IO) {
// 安全资源访问
file.outputStream().use { stream ->
try {
stream.write(data)
stream.flush()
} catch (e: IOException) {
// 错误处理
throw FileWriteException(e)
}
}
}
}
// 使用示例
viewModelScope.launch {
try {
writeToFile(data, cacheFile)
// 更新UI
withContext(Dispatchers.Main) {
showSuccess("File saved")
}
} catch (e: FileWriteException) {
// 错误处理
showError(e.message)
}
}
5.4.2 数据库事务安全
kotlin
suspend fun transferFunds(from: Account, to: Account, amount: Double) {
// 使用Room事务
withContext(Dispatchers.IO) {
database.runInTransaction {
// 1. 扣款
val newFromBalance = from.balance - amount
if (newFromBalance < 0) throw InsufficientFundsException()
accountDao.updateBalance(from.id, newFromBalance)
// 2. 存款
val newToBalance = to.balance + amount
accountDao.updateBalance(to.id, newToBalance)
// 3. 记录交易
transactionDao.insert(Transaction(from.id, to.id, amount))
}
}
}
5.5 性能优化策略
5.5.1 协程调度优化
scss
// 自定义线程池优化IO操作
private val dbDispatcher = Executors.newFixedThreadPool(4)
.asCoroutineDispatcher()
// 使用优化调度器
suspend fun loadData() {
// 并行执行
val userData = async(dbDispatcher) { userRepo.getUserData() }
val newsData = async(dbDispatcher) { newsRepo.getLatestNews() }
// 等待结果
val (user, news) = awaitAll(userData, newsData)
// 合并结果(在默认调度器)
withContext(Dispatchers.Default) {
processData(user, news)
}
// 更新UI(在主线程)
withContext(Dispatchers.Main) {
updateUI(user, news)
}
}
5.5.2 内存泄漏检测与预防
泄漏检测工具:
kotlin
// 在Application中启用协程调试
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
// 启用协程调试
System.setProperty("kotlinx.coroutines.debug", "on")
}
}
// 自定义泄漏检测器
object CoroutineLeakDetector {
private val activeJobs = ConcurrentHashMap<Job, Throwable>()
fun track(job: Job) {
activeJobs[job] = Throwable("Job created here")
}
fun checkLeaks() {
activeJobs.forEach { (job, stack) ->
if (!job.isActive && !job.isCompleted) {
Log.e("LeakDetector", "Potential coroutine leak", stack)
}
}
}
}
// 在基类ViewModel中使用
open class BaseViewModel : ViewModel() {
override fun onCleared() {
super.onCleared()
CoroutineLeakDetector.checkLeaks()
}
protected fun launchSafely(block: suspend CoroutineScope.() -> Unit): Job {
val job = viewModelScope.launch(block = block)
CoroutineLeakDetector.track(job)
return job
}
}
5.6 跨组件通信模式
5.6.1 基于SharedFlow的事件总线
kotlin
// 全局事件总线
object EventBus {
// 私有事件流(重放0,无缓存)
private val _events = MutableSharedFlow<Event>(
extraBufferCapacity = 100,
onBufferOverflow = BufferOverflow.DROP_OLDEST
)
// 公开只读流
val events = _events.asSharedFlow()
// 发送事件
suspend fun send(event: Event) {
_events.emit(event)
}
// 非阻塞发送
fun trySend(event: Event) {
_events.tryEmit(event)
}
}
// 发送登录事件
fun loginSuccess() {
EventBus.trySend(Event.LoginSuccess(user))
}
// 接收事件
class ProfileFragment : Fragment() {
private var eventsJob: Job? = null
override fun onStart() {
super.onStart()
eventsJob = lifecycleScope.launch {
EventBus.events
.filterIsInstance<Event.LoginSuccess>()
.collect { event ->
updateProfile(event.user)
}
}
}
override fun onStop() {
super.onStop()
eventsJob?.cancel()
}
}
5.6.2 Fragment间数据传递
kotlin
// 使用SavedStateHandle传递数据
class MainViewModel(
private val savedStateHandle: SavedStateHandle
) : ViewModel() {
// 状态保存
val selectionState = savedStateHandle.getStateFlow<SelectionState>(
"selection",
SelectionState.None
)
fun selectItem(item: Item) {
savedStateHandle["selection"] = SelectionState.Selected(item)
}
}
// FragmentA: 设置选择
fun onItemSelected(item: Item) {
findNavController().previousBackStackEntry
?.savedStateHandle
?.set("selection", item)
findNavController().popBackStack()
}
// FragmentB: 接收选择
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val savedStateHandle = navController.currentBackStackEntry?.savedStateHandle
savedStateHandle?.getStateFlow<Item>("selection")
?.onEach { item ->
// 更新UI
showSelectedItem(item)
}
?.launchIn(viewLifecycleOwner.lifecycleScope)
}
本章小结
本章深入探讨了协程与Android系统的深度整合:
- Jetpack组件集成:LifecycleScope与Room协程支持
- UI编程最佳实践:主线程安全访问与事件处理优化
- 后台任务处理:WorkManager协程支持
- 资源安全访问:文件与数据库操作安全模式
- 性能优化:调度策略与内存泄漏防护
- 跨组件通信:基于SharedFlow的事件总线与Fragment间数据传递
在下一章,我们将深入探讨协程的高阶并发模式,包括Actor模型实现、复杂管道设计、资源竞争解决方案以及结构化并发的进阶应用,帮助您构建高性能的Android应用架构。