-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[shared_preferences] Handling multiple files on Android #2040
Conversation
This PR is fine. I believe |
static void setMockInitialValues(Map<String, dynamic> values) { | ||
_kChannel.setMockMethodCallHandler((MethodCall methodCall) async { | ||
void setMockInitialValues(Map<String, dynamic> values) { | ||
throw UnimplementedError("Mocking function seems unfinished"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why you commented this out. Would it make sense to bring back? It is useful for unit testing apps that rely on the SharedPreferences plugin (although now that we have e2e testing that might be a reasonable alternative).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@collinjackson I don't understand the purpose of this function. As you can see, its implementation doesn't seem complete. What do you want me to do with this exactly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is used by apps that want to set mock SharedPreferences values for testing. It shouldn't be commented out.
@mehmetf Do you want me to do this myself? |
@axel-op Yes. We would not want to rollback the change because it introduced a regression. |
@mehmetf Here are my results with N = 10^5 (time is in microseconds):
Is this what you wanted? |
After creating some sort of cache on the Java side with a HashMap, here's what I get:
However I'm not sure these differences are significant |
Furthermore, I'm struggling with the |
… older Java versions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing the experiment. The numbers look find with caching.
packages/shared_preferences/shared_preferences/lib/shared_preferences.dart
Show resolved
Hide resolved
/// | ||
/// For Android, see https://developer.android.com/training/data-storage/shared-preferences.html for more details on the platform implementation. | ||
static Future<SharedPreferences> getInstance( | ||
{String filename = "FlutterSharedPreferences"}) async { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave this API alone and define a new one for Android specifically:
getAndroidPreferencesForFile(String preferencesFile)
Otherwise, it is misleading. Your application code will potentially contain multiple shared preferences instances and on iOS, these will clobber each other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created the method getInstanceForFile
, which will throw an AssertionError
if the current platform isn't Android. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Axel.
The more I think about it, the more I am worried about this API. What is your primary motivation?
- Do you intend to create multiple files backing different shared preferences?
- or are you in a migration scenario where you have an existing app that is using a different file name and you want to hook shared preferences plugin to it? (and you have no intention to use multiple files?)
The main problem with #1 is that multiple files is not supported on iOS so your app needs to be structured and behave differently on iOS when it comes to preferences. That's a tall order because this is not just a simple switch. For instance, on iOS, if you wanted to separate your preferences, you would need to namespace them. Let's say you were separating preferences for each user. You would do this on iOS:
user_id1:preference1
user_id1:preference2
user_id2:preference1
user_id2:preference2
but then you would write to separate files on Android? Why? You could do the same on Android too and you would not have to worry about differing behavior on different platforms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I apologize for not spotting this earlier. This PR has been open for a while and we did not give proper attention to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intention was to use multiple files in Flutter to separate the data. It seemed to me that it could be easier to manage because then you can use functions like clear
and getAll
on an entire file without the need to filter the preferences.
The reason why I didn't code the iOs-native part is because I don't have the knowledge to do so, but with your comment I now understand that it cannot be done at all. So it's probably better to not change the API indeed.
I am going to close this PR. Please see my update above date Nov 28 about my concerns with the API. If you want to do a migration from a non-conforming file, you can do so entirely in native code and then use this plugin. |
Description
This PR gives the possibility to use multiple files to save the SharedPreferences.
This for now only works on Android. On iOs, the previous behaviour is preserved.
See flutter/flutter#14337 (comment).
Related Issues
Fix flutter/flutter#14337
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]
). This will ensure a smooth and quick review process.-> one test seems to be unfinished or/and broken.
///
).flutter analyze
) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?