DataStore là sự thay thế hiện đại cho SharedPreferences, được xây dựng trên coroutine và Flow cho lưu trữ dữ liệu type-safe và transactional.
- Async-first (không blocking), hỗ trợ protocol buffer cho type safety, và an toàn hơn SharedPreferences vốn có vấn đề threading.
- Dùng
context.dataStore.data.map { it.setting }để đọc vàcontext.dataStore.updateData { it.copy(setting = value) }để ghi.
DataStore is a modern replacement for SharedPreferences, built on coroutines and Flow for type-safe, transactional data storage.
- It's async-first (no blocking), supports protocol buffers for type safety, and is safer than SharedPreferences which has threading issues.
- Use
context.dataStore.data.map { it.setting }to read andcontext.dataStore.updateData { ... }to write.