Coroutines guide - android coroutine API

 

1. snapshotFlow

 

 

1. snapshotFlow

fun <T : Any?> snapshotFlow(block: () -> T): Flow<T>

Create a Flow from observable Snapshot state. (e.g. state holders returned by mutableStateOf.)

snapshotFlow creates a Flow that runs block when collected and emits the result, recording any snapshot state that was accessed. While collection continues, if a new Snapshot is applied that changes state accessed by block, the flow will run block again, re-recording the snapshot state that was accessed. If the result of block is not equal to the previous result, the flow will emit that new result. (This behavior is similar to that of Flow.distinctUntilChanged.) Collection will continue indefinitely unless it is explicitly cancelled or limited by the use of other Flow operators.

 

snapshotFlow는 observable한 Snapshot state 로부터 flow를 만드는 것이다. 

즉, 스냅샷 형태인 block의 변경점을 기준으로 emitting을 하는 android dependent flow를 만들어주는 것.

 

그런데 여기서 말하는 Snapshot이란?(https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot)

 

Snapshot

A snapshot of the values return by mutable states and other state objects. All state object will have the same value in the snapshot as they had when the snapshot was created unless they are explicitly changed in the snapshot.

To enter a snapshot call enter. The snapshot is the current snapshot as returned by currentSnapshot until the control returns from the lambda (or until a nested enter is called). All state objects will return the values associated with this snapshot, locally in the thread, until enter returns. All other threads are unaffected.

Snapshots can be nested by calling takeNestedSnapshot.

 

변경 가능한 상태 및 기타 상태 개체가 반환하는 값의 스냅샷입니다. 모든 상태 개체는 스냅샷에서 명시적으로 변경되지 않는 한 스냅샷이 생성될 때와 동일한 값을 스냅샷에서 가집니다.

모든 상태 개체는 엔터가 반환될 때까지 이 스냅샷과 연결된 값을 스레드에서 로컬로 반환합니다. 다른 모든 스레드는 영향을 받지 않습니다.

 

 

  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유