I am writing an android app that is communicating with a webserver to get data. I decided to use protobuf and all works like a charm.
However my problem is this, my dataset is huge and I cant get all data at once so I just get the top level and then ask for more as I go along (browse on the phone).
I used protoc to generate my java class which works just fine and i get my nice shiny object structure that is just as i want it (initial with only top levels of data).
However I made the decision to use the structure I got as my main datastructure in my app and since all lists in that app are unmodifiable i cannot at a later date expand with more data and this is causing major headache for me.
For the moment I have simply edited the autogenerated java file that protoc generated to return normal lists rather than unmodifiable, but that is pretty dumb if i later need to regenerate that file.
what to do? not use protobuf object as storage? (just seems wasteful to copy all that data to a custom object when it is already there, not to mention writing my own storage classes.)
You should never modify the generated protocol buffer classes.
The message classes that are generated are immutable by design. To modify an object you need to use its corresponding
Builderclass to generate a new message from the existing one, manipulate it via its setter methods then callbuild()to return a modified object. For example:See “Builders vs. Messages” in the protobuf tutorial:
http://code.google.com/apis/protocolbuffers/docs/javatutorial.html#builders