Interview frequency: …
Short answer
Preferences DataStore stores key-value pairs with no predefined schema; Proto DataStore stores custom objects with strict, safe data types using Protocol Buffers.
Explanation
In Preferences DataStore there is no guarantee of the type safety of the data returned for each key, and runtime errors may occur when the type changes. In contrast, Proto DataStore defines the data structure precisely in a .proto file, and the compiler generates structured classes that are small and very fast at decoding, making it the best choice for complex, type-sensitive settings.
Code example
// 1. File user_prefs.proto:
// message UserPreferences {
// bool show_completed = 1;
// }
// 2. Fully type-safe reading in Kotlin:
val showCompletedFlow: Flow<Boolean> = context.userPreferencesStore.data
.map { preferences -> preferences.showCompleted }Common mistakes
- Using Proto DataStore for very simple settings made of two or three keys, where Protocol Buffers adds unnecessary setup and build overhead.