-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return unmodifiable string set from preferences #65
Conversation
@@ -12,7 +13,7 @@ | |||
static final StringSetAdapter INSTANCE = new StringSetAdapter(); | |||
|
|||
@Override public Set<String> get(@NonNull String key, @NonNull SharedPreferences preferences) { | |||
return preferences.getStringSet(key, null); | |||
return Collections.unmodifiableSet(preferences.getStringSet(key, null)); |
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.
oh, it's going to throw an npe. will fix.
Thanks! Changes LGTM. Behaviour wise, not 100% sure if it's the responsibility of the library to do this. Might be equally surprising for a user to see we're wrapping the set returned by the preferences. |
(I'm not too worries about the breaking changes part since v2 is relatively new, and might need some breaking changes with Flowables and nulls anyway) |
I think API calls for collections should normally return unmodifiable collections. Returning mutable collections is a surprising way to share state (and it's just an oversight/error for the Android sdk to do so). |
dd17e5b
to
f14ffb3
Compare
(I realized it's actually not a breaking change, since the default value is |
@NightlyNexus sorry forgot to follow up here. What's behaviour if you call |
That's actually the whole of the problem. From the doc:
Just sad legacy behavior in AOSP that it doesn't return an unmodifiable set, I believe. |
Got it. Ok let's add a small note about this in javadoc and |
Done. I just made a quick edit to the javadoc and am never offended if you want to tweak any wording, etc. haha. |
LGTM! |
https://developer.android.com/reference/android/content/SharedPreferences.html#getStringSet(java.lang.String,%20java.util.Set%3Cjava.lang.String%3E)
This can cause subtle errors and ConcurrentModificationExceptions if the set is mutated by the caller.
This is a breaking change, but I thought I'd propose it, anyway. :)
Also, it could just return a mutable copy (
new LinkedHashSet<>()
instead ofunmodifiableSet()
) to be a compatible change.