Jetpack DataStoreを消去する
公式に良いのが見つからなかったのでメモ
- Preferences DataStore
// 消去 context.dataStore.edit { it.clear() } // 定義 val Context.dataStore by preferencesDataStore(name = "settings")
- Proto DataStore
// 消去 context.datastore.updateData { it.toBuilder().clear().build() } // 定義 object SettingsSerializer : Serializer<Settings> { override val defaultValue: Settings = Settings.getDefaultInstance() override suspend fun readFrom(input: InputStream): Settings { try { return Settings.parseFrom(input) } catch (exception: InvalidProtocolBufferException) { throw CorruptionException("Cannot read proto.", exception) } } override suspend fun writeTo( t: Settings, output: OutputStream) = t.writeTo(output) } val Context.settingsDataStore: DataStore<Settings> by dataStore( fileName = "settings.pb", serializer = SettingsSerializer )
android - How to clear jetpack datastore data on specific condition - Stack Overflow
- Proto DataStore
https://developer.android.com/topic/libraries/architecture/datastore#proto-create
- codelab