Thread
#compose
    Law Gimenez

    Law Gimenez

    1 year ago
    Hi friends, tried migrating to DataStore from SharedPreferences but
    createDataStore
    method not found. I already added the library on Gradle. I’m following Google’s documentation by the way
    p

    patrick

    1 year ago
    Not sure which version you are using, but there's something about this in the Release Notes of DataStore for the alpha07: https://developer.android.com/jetpack/androidx/releases/datastore#1.0.0-alpha07
    Law Gimenez

    Law Gimenez

    1 year ago
    Thanks I’m checking this one https://android-developers.googleblog.com/2020/09/prefer-storing-data-with-jetpack.html. Do you have any idea how to migrate from SharedPreferences using this latest syntax?
    p

    patrick

    1 year ago
    Does this work:
    val Context.myDataStore by preferencesDataStore(
        name = "settings",
        produceMigrations = { context ->
            listOf(
                SharedPreferencesMigration(context, "settings")
            )
        }
    )
    ?
    Law Gimenez

    Law Gimenez

    1 year ago
    @patrick no it doesn’t work I’m sorry
    p

    patrick

    1 year ago
    In your context it could look somewhat like this:
    val Context.myDataStore by preferencesDataStore(
        name = "filename",
        produceMigrations = { context ->
            listOf(
                SharedPreferencesMigration(context, "settings")
            )
        }
    )
    
    object OnlineJobPreferences{
    
        fun save(context: Context, jobPreferences: String){
            val dataStore = context.myDataStore
            dataStore.updateData { ... }
        }
    }
    You can access the datastore anywhere in your codebase by calling
    myDataStore
    on a context and the migration should happen automatically
    Law Gimenez

    Law Gimenez

    1 year ago
    @patrick thanks your syntax worked already. Does it migrate all the stored values from my old SharedPreferences, or should do it manually?
    p

    patrick

    1 year ago
    It should work automatically you just need to use the datastore (reading or writing) as I understood it from the docs
    Law Gimenez

    Law Gimenez

    1 year ago
    @patrick appreciate the help. Thank you so much!