Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear jetpack datastore data on specific condition

i've been using jetpack datastore for a while, but then i got a problem. I want to clear data in datastore when the app is destroyed. Im using jetpack datastore to persist data only in form

i've searched that sharedPreferences has a clear() function, is there a similar function for Jetpack Datastore ? and how can i use it ?

i found clear function in datastore documentation but there is no explanation on how to use it

like image 414
Pif Avatar asked Nov 16 '20 01:11

Pif


2 Answers

Use this

dataStore.edit { 
        it.clear()
    }

Method description states

Removes all preferences from this MutablePreferences.

For proto datastore (Thanks to Amir Raza for comment)

datastore.updateData { 
    it.toBuilder().clear().build()
}
like image 193
Manohar Avatar answered Nov 15 '22 23:11

Manohar


In case anyone wants to know how to remove a specific preference

context.dataStore.edit {
    it.remove(key)
}
like image 30
Wirling Avatar answered Nov 15 '22 23:11

Wirling