告別SharedPreferences!用DataStore打造更靠譜的本地存儲
DataStore
作為Android官方推薦的新一代數(shù)據(jù)存儲方案,完美解決了SharedPreferences
的三大痛點(diǎn):
1. 主線程卡頓終結(jié)者
所有讀寫操作自動切到子線程,再也不用擔(dān)心用戶點(diǎn)按鈕時卡住界面。對比之下SharedPreferences
的commit()
方法就像在早高峰的主干道上調(diào)頭。
2. 類型安全不翻車
用ProtoDataStore
可以像寫類一樣定義數(shù)據(jù)結(jié)構(gòu),徹底告別SharedPreferences
里把字符串當(dāng)數(shù)字用的尷尬場景。
3. 數(shù)據(jù)保險箱機(jī)制
自帶"原子操作"屬性,就算突然斷電也不會出現(xiàn)存了一半的殘缺數(shù)據(jù)。這就像銀行轉(zhuǎn)賬,要么成功要么失敗,絕不會有中間狀態(tài)。
手把手教你兩種用法
簡單配置存儲(Preferences版)
適用場景:比如記住用戶設(shè)置的夜間模式、字體大小等簡單配置
// 第一步:在build.gradle添加
dependencies {
implementation 'androidx.datastore:datastore-preferences:1.1.1'
}
// 第二步:創(chuàng)建存儲文件(放在Application類里)
val Context.appSettings by preferencesDataStore(name = "user_prefs")
// 第三步:定義要存的字段
object PrefsKeys {
val DARK_MODE = booleanPreferencesKey("dark_mode")
val FONT_SIZE = intPreferencesKey("font_size")
}
// 第四步:讀寫操作
classSettingsRepository(privateval context: Context) {
// 讀取設(shè)置
val darkModeFlow: Flow<Boolean> = context.appSettings.data
.map { prefs -> prefs[PrefsKeys.DARK_MODE] ?: false }
// 修改設(shè)置
suspendfun toggleDarkMode(enable: Boolean) {
context.appSettings.edit { settings ->
settings[PrefsKeys.DARK_MODE] = enable
}
}
}
復(fù)雜數(shù)據(jù)存儲(Proto版)
適用場景:存儲用戶游戲存檔、購物車信息等結(jié)構(gòu)化數(shù)據(jù)
// 第一步:定義proto結(jié)構(gòu)(新建settings.proto文件)
syntax = "proto3";
message GameSave {
int32 current_level = 1;
repeated string unlocked_items = 2;
map<string, int32> equipment_stats = 3;
}
// 第二步:生成Java類(Build -> Rebuild Project)
// 第三步:實(shí)現(xiàn)序列化器
object GameSaveSerializer : Serializer<GameSave> {
overrideval defaultValue = GameSave.getDefaultInstance()
overridesuspendfun readFrom(input: InputStream) =
try { GameSave.parseFrom(input) }
catch (e: Exception) { throw CorruptionException("存檔損壞", e) }
overridesuspendfun writeTo(data: GameSave, output: OutputStream) =
data.writeTo(output)
}
// 第四步:創(chuàng)建DataStore實(shí)例
val Context.gameData by dataStore("game_saves.pb", GameSaveSerializer)
// 第五步:操作游戲存檔
classGameSaveManager(privateval context: Context) {
// 讀取關(guān)卡進(jìn)度
val currentLevelFlow: Flow<Int> =
context.gameData.data.map { it.currentLevel }
// 解鎖新道具
suspendfun unlockItem(itemName: String) {
context.gameData.updateData { current ->
current.toBuilder()
.addUnlockedItems(itemName)
.build()
}
}
}
開發(fā)避坑指南
1.單例原則
每個存儲文件只能創(chuàng)建一個DataStore
實(shí)例,建議在Application
類初始化
2.遷移老數(shù)據(jù)dataStore.migrateFrom(sharedPrefs)
一鍵遷移,記得先停用舊的SharedPreferences
3.多進(jìn)程場景
用這個特殊創(chuàng)建方式保證數(shù)據(jù)安全:
val multiProcessStore = MultiProcessDataStoreFactory.create(
serializer = GameSaveSerializer(),
produceFile = { File(context.filesDir, "multi_process_data.pb") }
)
4.異常處理
在Flow
收集時加上catch
處理:
context.gameData.data
.catch { ex ->
if (ex is CorruptionException) recoverFromCorruption()
}
.collect { gameSave -> updateUI(gameSave) }
什么時候該用DataStore?
- 需要存用戶個性化設(shè)置
- 需要緩存接口返回的簡單數(shù)據(jù)
- 需要保存應(yīng)用狀態(tài)(比如表單草稿)
- 需要跨進(jìn)程共享數(shù)據(jù)
?? 需要存大量結(jié)構(gòu)化數(shù)據(jù) → 考慮Room數(shù)據(jù)庫
?? 需要存圖片/視頻 → 直接用文件存儲
升級到DataStore后,你會發(fā)現(xiàn)代碼里少了無數(shù)個getSharedPreferences(),再也不用寫commit()和apply()的糾結(jié)選擇,數(shù)據(jù)操作就像用LiveData一樣流暢自然。