Fixed
Status Update
Comments
fl...@google.com <fl...@google.com> #2
To add more details: When migrating, we implement something like this:
SharedPreferencesMigration(
context,
USER_PREFERENCES_NAME
) { sharedPrefs: SharedPreferencesView, currentData: UserPreferences ->
// Define the mapping from SharedPreferences to UserPreferences
if (...) {
currentData.toBuilder().setSortOrder(
SortOrder.valueOf(
sharedPrefs.getString(SORT_ORDER_KEY, SortOrder.NONE.name)!!
)
).build()
} else {
currentData
}
}
In this case, if SORT_ORDER_KEY is not already saved in SharedPreferences the app crashes with "Can't access key outside migration", even if we defined a default value.
The expectation here was that sharedPrefs.getString(SORT_ORDER_KEY, SortOrder.NONE.name)
returns SortOrder.NONE.name
if the key isn't in the preferences.
ap...@google.com <ap...@google.com> #3
Project: platform/frameworks/support
Branch: androidx-main
commit 247fd51abbf6a6804d65a833d22423eef349d036
Author: Rohit Sathyanarayana <rohitsat@google.com>
Date: Wed Jul 07 13:33:40 2021
Stop throwing exceptions when a key doesn't exist when all keys is specified for SharedPreferencesMigration
Bug: 192824325
Relnote: Fix bug where SharedPreferencesMigration constructed with MIGRATE_ALL_KEYS would throw an exception if the key requested does not yet exist.
Test: new unit test
Change-Id: Ie318a9eb3772f5ef6dd231c18837842b887587e0
M buildSrc/src/main/kotlin/androidx/build/LibraryVersions.kt
M datastore/datastore/src/androidTest/java/androidx/datastore/migrations/SharedPreferencesMigrationTest.kt
M datastore/datastore/src/main/java/androidx/datastore/migrations/SharedPreferencesMigration.kt
https://android-review.googlesource.com/1758106
Branch: androidx-main
commit 247fd51abbf6a6804d65a833d22423eef349d036
Author: Rohit Sathyanarayana <rohitsat@google.com>
Date: Wed Jul 07 13:33:40 2021
Stop throwing exceptions when a key doesn't exist when all keys is specified for SharedPreferencesMigration
Bug: 192824325
Relnote: Fix bug where SharedPreferencesMigration constructed with MIGRATE_ALL_KEYS would throw an exception if the key requested does not yet exist.
Test: new unit test
Change-Id: Ie318a9eb3772f5ef6dd231c18837842b887587e0
M buildSrc/src/main/kotlin/androidx/build/LibraryVersions.kt
M datastore/datastore/src/androidTest/java/androidx/datastore/migrations/SharedPreferencesMigrationTest.kt
M datastore/datastore/src/main/java/androidx/datastore/migrations/SharedPreferencesMigration.kt
ap...@google.com <ap...@google.com> #4
Project: platform/frameworks/support
Branch: androidx-main
commit 52a6dc065e4b794b7c40c208fcad6edd359d9b56
Author: Rohit Sathyanarayana <rohitsat@google.com>
Date: Thu Jul 08 16:24:40 2021
Add documentation to SharedPreferencesMigration
This was brought up in aosp/1758106 but I forgot to upload before submitting...
Relnote: clarify that SharedPreferencesMigration does not run if there are no keys.
Test: N/A
Bug: 192824325
Change-Id: Icfa327c72834bab2a53be8f3696f48c19781c93f
M datastore/datastore-rxjava2/src/main/java/androidx/datastore/rxjava2/RxSharedPreferencesMigration.kt
M datastore/datastore-rxjava3/src/main/java/androidx/datastore/rxjava3/RxSharedPreferencesMigration.kt
M datastore/datastore/src/main/java/androidx/datastore/migrations/SharedPreferencesMigration.kt
https://android-review.googlesource.com/1760672
Branch: androidx-main
commit 52a6dc065e4b794b7c40c208fcad6edd359d9b56
Author: Rohit Sathyanarayana <rohitsat@google.com>
Date: Thu Jul 08 16:24:40 2021
Add documentation to SharedPreferencesMigration
This was brought up in aosp/1758106 but I forgot to upload before submitting...
Relnote: clarify that SharedPreferencesMigration does not run if there are no keys.
Test: N/A
Bug: 192824325
Change-Id: Icfa327c72834bab2a53be8f3696f48c19781c93f
M datastore/datastore-rxjava2/src/main/java/androidx/datastore/rxjava2/RxSharedPreferencesMigration.kt
M datastore/datastore-rxjava3/src/main/java/androidx/datastore/rxjava3/RxSharedPreferencesMigration.kt
M datastore/datastore/src/main/java/androidx/datastore/migrations/SharedPreferencesMigration.kt
ro...@google.com <ro...@google.com> #5
Thanks for reporting this Florina. I've updated the code so that it will no longer crash when the key doesn't exist if it is constructed with MIGRATE_ALL_KEYS. The change will go out in rc2.
fl...@google.com <fl...@google.com> #6
Thanks, Rohit!
Description
DataStore Component used: Proto DataStore (but behaviour should be common to Preferences as well given the scenario mentioned below)
DataStore Version used: rc01
Devices/Android versions reproduced on: all
Consider the following scenario: In SharedPreferences we have preference with key A saved. The app supports a preference with key B as well, but the user didn't get to the screen in the app where that is actually saved, therefore SharedPreferences is missing key B.
We implement a
SharedPreferencesMigration
, using the defaultMIGRATE_ALL_KEYS
option and callsharedPreferencesView.get...(key = B)
to define the mapping between SharedPrefs and DataStore. When migrating, we get "Can't access key outside migration".MIGRATE_ALL_KEYS
creates a set of keys containing just key "A", as this was the only one set by the user. The solution to this is to manually define the list of keys to be migrated.But, this is counter-intuitive to the
MIGRATE_ALL_KEYS
flag, as it assumes that all the keys are already saved in SharedPreferences.