Kotlin Flow

2022-07-17  本文已影响0人  TomyZhang

一、Flow

1.Flow 是什么?

2.如何创建 Flow?

3.如何切换线程?

private fun testFlow() {
    CoroutineScope(Job()).launch {
        Log.d(TAG, "zwm, launch thread: ${Thread.currentThread().name}")
        flow {
            for (i in 1..3) {
                delay(500)
                Log.d(TAG, "zwm, emit: $i thread: ${Thread.currentThread().name}")
                emit(i)
            }
        }.flowOn(Dispatchers.IO)
                .collect {
                    Log.d(TAG, "zwm, collect: $it thread: ${Thread.currentThread().name}")
                }
    }
}

//日志打印
2022-07-16 20:54:55.376 13260-13337/com.tomorrow.target30 D/KtActivity: zwm, launch thread: DefaultDispatcher-worker-1
2022-07-16 20:54:55.889 13260-13338/com.tomorrow.target30 D/KtActivity: zwm, emit: 1 thread: DefaultDispatcher-worker-2
2022-07-16 20:54:55.892 13260-13337/com.tomorrow.target30 D/KtActivity: zwm, collect: 1 thread: DefaultDispatcher-worker-1
2022-07-16 20:54:56.393 13260-13337/com.tomorrow.target30 D/KtActivity: zwm, emit: 2 thread: DefaultDispatcher-worker-1
2022-07-16 20:54:56.395 13260-13338/com.tomorrow.target30 D/KtActivity: zwm, collect: 2 thread: DefaultDispatcher-worker-2
2022-07-16 20:54:56.895 13260-13338/com.tomorrow.target30 D/KtActivity: zwm, emit: 3 thread: DefaultDispatcher-worker-2
2022-07-16 20:54:56.897 13260-13337/com.tomorrow.target30 D/KtActivity: zwm, collect: 3 thread: DefaultDispatcher-worker-1

4.操作符有哪些?

二、冷流与热流

1.冷流是什么?

在 Kotlin 中,Flow 是冷流。只有订阅者订阅时,才开始执行发射数据流的代码。并且冷流和订阅者只能是一对一的关系,当有多个不同的订阅者时,消息是重新完整发送的。也就是说对冷流而言,有多个订阅者的时候,它们各自的事件是独立的。

2.热流是什么?

在 Kotlin 中,StateFlow、SharedFlow 是热流。无论有没有订阅者订阅,事件始终都会发生。当热流有多个订阅者时,热流与订阅者们的关系是一对多的关系,可以与多个订阅者共享信息。

三、StateFlow

1.StateFlow 是什么?

StateFlow 是一个状态容器式可观察数据流,可以向其收集器发出当前状态更新和新状态更新。可以通过其 value 属性读取当前状态值,如需更新状态并将其发送到数据流,那么就需要使用 MutableStateFlow。

2.StateFlow 如何使用?

private fun testFlow() {
    val data = MutableStateFlow(100) //需要有初始值
    CoroutineScope(Job()).launch {
        Log.d(TAG, "zwm, launch thread: ${Thread.currentThread().name}")
        data.collect {
            Log.d(TAG, "zwm, collect thread: ${Thread.currentThread().name}, data: ${data.value}")
        }
    }

    Handler(Looper.getMainLooper()).postDelayed(Runnable {
        Log.d(TAG, "zwm, postDelayed thread: ${Thread.currentThread().name}")
        CoroutineScope(Job()).launch {
            Log.d(TAG, "zwm, launch thread: ${Thread.currentThread().name}")
            Log.d(TAG, "zwm, current data: ${data.value}")
            data.value = 200 //上一个值为 100,更新值为 200,正常接收
//            data.value = 100 //上一个值为 100,更新值为 100,不接收
        }
    }, 3000)
}

//日志打印
2022-07-17 10:35:13.175 3097-3325/com.tomorrow.target30 D/KtActivity: zwm, launch thread: DefaultDispatcher-worker-1
2022-07-17 10:35:13.176 3097-3325/com.tomorrow.target30 D/KtActivity: zwm, collect thread: DefaultDispatcher-worker-1, data: 100
2022-07-17 10:35:16.174 3097-3097/com.tomorrow.target30 D/KtActivity: zwm, postDelayed thread: main
2022-07-17 10:35:16.176 3097-3325/com.tomorrow.target30 D/KtActivity: zwm, launch thread: DefaultDispatcher-worker-1
2022-07-17 10:35:16.176 3097-3325/com.tomorrow.target30 D/KtActivity: zwm, current data: 100
2022-07-17 10:35:16.178 3097-3326/com.tomorrow.target30 D/KtActivity: zwm, collect thread: DefaultDispatcher-worker-2, data: 200

四、SharedFlow

1.SharedFlow 是什么?

SharedFlow 提供了 SharedFlow 与 MutableSharedFlow 两个版本,平时使用较多的是 MutableSharedFlow。MutableSharedFlow 没有起始值,发送数据时需要调用 emit()、tryEmit() 方法。构造函数如下:

public fun <T> MutableSharedFlow(
    replay: Int = 0,
    extraBufferCapacity: Int = 0,
    onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND
): MutableSharedFlow<T>

2.SharedFlow 如何使用?

private fun testFlow() {
    val data = MutableSharedFlow<Int>() //不需要初始值
    CoroutineScope(Job()).launch {
        Log.d(TAG, "zwm, launch thread: ${Thread.currentThread().name}")
        data.collect {
            Log.d(TAG, "zwm, collect thread: ${Thread.currentThread().name}, data: $it")
        }
    }

    Handler(Looper.getMainLooper()).postDelayed(Runnable {
        Log.d(TAG, "zwm, postDelayed thread: ${Thread.currentThread().name}")
        CoroutineScope(Job()).launch {
            Log.d(TAG, "zwm, launch thread: ${Thread.currentThread().name}")
            data.emit(200)
            data.emit(200) //上一个值为 200,更新值为 200,正常接收
        }
    }, 3000)
}

//日志打印
2022-07-17 10:49:00.782 5908-6008/com.tomorrow.target30 D/KtActivity: zwm, launch thread: DefaultDispatcher-worker-1
2022-07-17 10:49:03.782 5908-5908/com.tomorrow.target30 D/KtActivity: zwm, postDelayed thread: main
2022-07-17 10:49:03.787 5908-6008/com.tomorrow.target30 D/KtActivity: zwm, launch thread: DefaultDispatcher-worker-1
2022-07-17 10:49:03.793 5908-6010/com.tomorrow.target30 D/KtActivity: zwm, collect thread: DefaultDispatcher-worker-2, data: 200
2022-07-17 10:49:03.801 5908-6010/com.tomorrow.target30 D/KtActivity: zwm, collect thread: DefaultDispatcher-worker-2, data: 200

3.StateFlow 与 SharedFlow 的区别是什么?

上一篇下一篇

猜你喜欢

热点阅读