안드로이드 초기부터 사용해오던 SharedPreference 에는 단점이 존재

  • UI thread 에서 호출하기에는 thread safe 하지 않음 – disk I/O operations 을 포함하기 때문. (Async API 를 제공하기는 하지만…)
  • parsing error 를 던지는 runtime exception 에서 안전하지 않음
  • Cannot signal errors
  • 저장되는 데이터의 Type-safety 를 제공하지 않음
  • Data storage 의 consistency 를 위한 transactional API 를 제공하지 않음

JetPack 에서는 이런 단점을 보완하기 위하여 Preference 대체용 DataStore 를 제공

https://android-developers.googleblog.com/

DataStore 는 Kotlin coroutine, flow 를 이용하여 3가지 장점(비동기, 일관성, 트랜잭션 – asynchronously, consistently, and transactionally)을 제공하며, 아래와 같은 2가지 형태의 구현이 가능.

  • Preferences DataStore: Stores data in key-value pairs similar to Shared Preferences. It doesn’t provide any type-safety. No predefined schema is required.
  • Proto DataStore: Stores data as instances of a custom data type with predefined schema using Protocol buffers. It provides type-safety.

type-safety 를 제공하느냐에 따라 Preference, Proto 로 구분됨


참고