question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proper null handling

See original GitHub issue

I need to be able to observe a preference’s value (or lack of value or if it has been cleared). It’s very important to be able to keep the stream alive.

Observable<Optional<Boolean>> //note Optional is my own custom class

Would it be possible to pass in some function to handle a custom mapping when the preference is null?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ZakTaccardicommented, Jul 3, 2017

Use a sentinel value for the default. When the preference does not exist, the default value is emitted. Check the value against the sentinel value, if they’re equal the value does not exist.

This causes two problems

  • Can’t use a sentinel value for a Boolean optional. We only have true and false
  • Duplicating extra work across users of this library. Ideally, the library itself should bear this burden

I know RxJava2’s lack of nullability without a proper optional is rather an annoying problem to solve. Worst case scenario, an optional implementation (maybe call it EmptyPreference<T>) would be better than nothing.

1reaction
ZakTaccardicommented, Jul 19, 2017

You can use integers or enums or a custom object instead.

This could certainly work, but does android treat Boolean vs String preferences differently? What happens if you access a Boolean preference with sharedPrefs.getString(key) where the key already exists in a boolean preference. Does it just return true?

I need to be able to completely interop with the other non-reactive ways the app (co-workers) access the preference.

Ultimately I still suggest some type of converter a la Retrofit style to enable usage of custom Optional<T> types.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java 8 Optional: Handling Nulls Properly - DZone
In this tutorial, learn how to handle null properties in Java 8 by using the Optional class to help reduce the number of...
Read more >
Avoid Check for Null Statement in Java - Baeldung
Generally, null variables, references and collections are tricky to handle in Java code. They are not only hard to identify but also complex...
Read more >
Better Null-Handling With Java Optionals - belief driven design
Until Java 8, there was no better way to handle null references than checking your variables at every turn. The new class java.util....
Read more >
10. Handling nulls with Optional - Java 8 tutorial!
You can create an optional object from a nullable value using the static factoy method Optional.ofNullable . The advantage over using this method...
Read more >
Best way to handle nulls in Java? [closed]
If the data comes from outside your control, systematically check for nulls and act appropriately. · If Null is within the domain of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found