CBLJSONEncoding Usage

269 views
Skip to first unread message

Ragu Vijaykumar

unread,
Apr 10, 2014, 9:37:14 AM4/10/14
to mobile-c...@googlegroups.com
I play to use this, but had a question regarding its implementation. Looking at the doxygen files, I see that there are 2 methods: initWithJSON, encodeAsJSON.


However, looking at the source header CBLJSON.h, I see that there is an additional an additional optional method declared, setOnMutate:(CBLOnMutateBlock)onmutate - I'm still a bit unclear on when to implement / utilize this method properly.

Does this mean that if my object implementing CBLJSONEncoding is at all mutable, I need this class? And if so, does it mean that anytime I change any of its instance variables / properties, I need to call the onMutate block?

If the above answers are yes, is there any clean way I can just observe changes to all my properties and automatically call this block, perhaps as a generic superclass to any nested child classes of a CBLModel?

Thanks,
Ragu

Ragu Vijaykumar

unread,
Apr 10, 2014, 9:39:37 AM4/10/14
to mobile-c...@googlegroups.com
Oh, and one more question:

Is it possible for CBLJSONEncoding objects to have a property whose type is itself another CBLJSONEncoding, or an array of CBLJSONEncoding objects? Thanks!

Ragu

Jens Alfke

unread,
Apr 10, 2014, 10:01:01 AM4/10/14
to mobile-c...@googlegroups.com
On Apr 10, 2014, at 6:37 AM, Ragu Vijaykumar <ra...@scrxpt.com> wrote:

Does this mean that if my object implementing CBLJSONEncoding is at all mutable, I need this class? And if so, does it mean that anytime I change any of its instance variables / properties, I need to call the onMutate block?

Yes. The parent model object needs to know when its in-memory state has changed, so that it can set its needsSave flag. Otherwise it won’t get saved back to the document it came from. Normally it can do that easily by just noticing when any of its property setters are called. But if a property value is a custom class implementing CBLJSONEncoding, then that value might itself be mutable and the model can’t tell when it changes.

So what your class should do is implement that method and store the onMutate block in an instance variable, then call that block right after its persistent state changes. That will notify the model, which will set its own needsSave flag.

If the above answers are yes, is there any clean way I can just observe changes to all my properties and automatically call this block, perhaps as a generic superclass to any nested child classes of a CBLModel?

Interesting idea. I think that would work. You might even be able to use the Obj-C runtime’s reflection APIs to automatically discover all of the class’s mutable properties and iterate over them. Let us know if you come up with something.

—Jens
Message has been deleted

Ragu Vijaykumar

unread,
Apr 12, 2014, 5:27:38 PM4/12/14
to mobile-c...@googlegroups.com
I was looking through the code and I saw that the onMutateBlock seems to only ever be set in a CBLJSONEncoding object when the CBLModel is loading its contents from a CBLDocument. But what about the first time the CBLJSONEncoding object is created and assigned to a CBLModel? Let's say the model is saved, program is still running, user makes changes to the CBLJSONEncoding child object - how does one notify the parent CBLModel that it needs to be saved since the onMutateBlock was never set?

Pseudo-code:
CBLModel* model = new CBLModel;
CBLJSONEncoding childModel = new CBLJSONEncoding;
model.childModelProperty = childModel;
model.save;

childModel.property = new value;
childModel.onMutateBlock();                                       // Supposed to do, but onMutateBlock is nil

Thanks for all your help!

Cheers,
Ragu

Jens Alfke

unread,
Apr 12, 2014, 5:42:48 PM4/12/14
to mobile-c...@googlegroups.com

On Apr 12, 2014, at 2:27 PM, Ragu Vijaykumar <ra...@scrxpt.com> wrote:

I was looking through the code and I saw that the onMutateBlock seems to only ever be set in a CBLJSONEncoding object when the CBLModel is loading its contents from a CBLDocument. But what about the first time the CBLJSONEncoding object is created and assigned to a CBLModel?

Hm, I don’t have time right now to dig into the source, but it sounds like this may be a bug. Could you file an issue on Github, please? I’ll take a look soon.

—Jens

Message has been deleted

Ragu Vijaykumar

unread,
Apr 13, 2014, 12:06:00 AM4/13/14
to mobile-c...@googlegroups.com
No problem - I posted the issue there. I also wrote a class I hope can serve to replace that mechanism, called CBLNestedModel. How can I submit this code to the project? It automatically encodes/decodes the class from JSON, and allows multiple levels of nesting as well so people won't have to write their own. Thanks.

Ragu

Jens Alfke

unread,
Apr 13, 2014, 12:25:41 PM4/13/14
to mobile-c...@googlegroups.com

On Apr 12, 2014, at 9:06 PM, Ragu Vijaykumar <ra...@scrxpt.com> wrote:

No problem - I posted the issue there. I also wrote a class I hope can serve to replace that mechanism, called CBLNestedModel. How can I submit this code to the project? It automatically encodes/decodes the class from JSON, and allows multiple levels of nesting as well so people won't have to write their own. Thanks.

Send a pull request, please, and I can look at it. Thanks!

—Jens

Ragu Vijaykumar

unread,
Apr 14, 2014, 2:58:16 PM4/14/14
to mobile-c...@googlegroups.com
I just submitted a pull request for a bunch of added functionality. I added support for NSDictionaries, and created a brand new superclass called CBLNestedModel which allows N-depth level storage of information within a CBLModel. Please let me know if you have any questions about it.

Cheers,
Ragu
Reply all
Reply to author
Forward
0 new messages