When you have a big POJO with loads of variables (Booleans, Int, Strings) and you want to use the new Work Manager to start a job. You then create a Data file which gets added to the one time work request object.
What would be the best practices to build this data file? (It feels wrong to be writing 100 lines of code to just say put int on the builder for every variable.)
Answer
I ended up breaking apart my parcelable object as i thought this was the best implementation. I did not want to use gson lib as it would of added another layer of serialization to my object.
Data.Builder builder = new Data.Builder();
builder.putBoolean(KEY_BOOL_1, stateObject.bool1);
builder.putBoolean(KEY_BOOL_2, stateObject.bool2);
builder.putBoolean(KEY_BOOL_3, stateObject.bool3);
builder.putInt(KEY_INT_1, stateObject.int1);
builder.putInt(KEY_INT_2, stateObject.int2);
builder.putString(KEY_STRING_1, stateObject.string1);
return builder.build();
UPDATE
The partial answer to my question is as @CommonsWare pointed out :
The reason Parcelable is not supported is that the data is persisted.
Not sure what the detailed answer to Data not supporting parcelable?
- This answer explains its :
The Data is a lightweight container which is a simple key-value map and can only hold values of primitive & Strings along with their String version. It is really meant for light, intermediate transfer of data. It shouldn't be use for and is not capable of holding Serializable or Parcelable objects.
Do note, the size of data is limited to 10KB when serialized.