How I handled null values in Android Proto DataStore?

Jetpack DataStore is new data storage solution in Android that would replace SharedPreference in future. DataStore allows you to store key-value pairs or typed objects with protocol buffers.
If you’re currently using SharedPreferences to store data, so it’s good time to start migration to DataStore instead.

DataStore provides two different implementations: Preferences DataStore and Proto DataStore.

I was very excited to know practically how this new storage solution (DataStore) works. So in the Christmas holidays, I made a sample project on Proto DataStore to learn and improve my skills.

While working with DataStore i had multiple issues, but the major and most challenging issue was supporting null values in Proto DataStore.
We can’t store null values in Proto DataStore instead we have other solutions.

I found following solutions to add Null support in proto3, you can use according to your use-case.
(I will add more solutions as soon as i will come to know)

Method 1

We have wrappers.proto to support null in protobuffers

To use above wrappers you need to import “google/protobuf/wrappers.proto”;
And to declare nullable string use StringValue,

Method 2

We can use Oneof to define a nullable type, as of my understanding the best way of using this is 👇🏼

But in this case, you have to do a lot of additional stuff, like handling null via if else in service implementation.

Method 3 (using Default Values — Recommended)

In proto3 all fields are optional by default so each field have default value
These defaults are type-specific:

  • For strings, the default value is the empty string.
  • For bytes, the default value is empty bytes.
  • For bools, the default value is false.
  • For numeric types, the default value is zero.
  • For enums, the default value is the first defined enum value, which must be 0.
  • For message fields, the field is not set. Its exact value is language-dependent. See the generated code guide for details.

We have to handle these default values using conditional code like if object is null pass its default value instead.

Thank you!!

That’s all from my learnings!!

Hey!!, I am ready to add more methods , if you know something different to handle null in protocol-buffers, Please let me know. i will be very thankful to you. you can find me on LinkedIn.

--

--

--

Senior Android Engineer @ Nect

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Tutorial Node.js: Create REST API Calculator

Angular13 with Firebase Remote Config

Firebase console for Remote Config

Reactime Version 12 Updates

ReactJS and jQuery are not comparable

Simplifying React form state management

JavaScript Aesthetics 101: Style Rules For Beautiful Code

Most Important JavaScript Questions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ishrat khan

Ishrat khan

Senior Android Engineer @ Nect

More from Medium

Async Jersey + Kotlin Coroutines

Null Safety in Kotlin

Looking Back Into MessageQueue, Looper and Handler Internal💫

Watch files on folder with Kotlin