pwittchen / prefser

Wrapper for Android SharedPreferences with object serialization and RxJava Observables
Apache License 2.0
229 stars 26 forks source link

Accept null value for put() to remove the key, fix NPE on null default value #50

Closed ypresto closed 9 years ago

ypresto commented 9 years ago

sharedPreferences.putString(key, null) removes that key. This PR implements this in prefser. Also fixes that null default value for primitive data types (prefser.get(key, Integer.class, null)) causes NPE. Thanks!

ypresto commented 9 years ago

Updated PR to include fix NPE on null default value.

pwittchen commented 9 years ago

Thank you for your Pull Request! I really appreciate it. Nevertheless, I would like to avoid passing null as a parameters in the library. It's not recommended practice. The only exception is a situation in which Android API or native Java method allows to pass null, but it's still not good. Moreover, removing values with put(...) method may be confusing for some developers, which may expect null value to be stored under a specified key. I'd like developers to explicitly use remove(...) method when they want to remove some value.

If null value was passed as a default value, NPE can occur and developer should be aware of that. SharedPreferences in Android API have the same behavior. If you would like to avoid NPE, you should pass default value different than null.

pwittchen commented 9 years ago

I see, you are right about test dependencies, which fixes execution of the unit tests, but unfortunately I cannot merge part of this PR and I've updated dependencies in a separate commit.