CBLJSONEncoding - (instancetype)initWithJSON:(id)jsonObject not called when [CBLModelSubclass modelForDocument: doc]

151 views
Skip to first unread message

SeanDev

unread,
Mar 13, 2015, 5:51:50 AM3/13/15
to mobile-c...@googlegroups.com


Hi Guys,

I've been experimenting on CBLJSONEncoding which needs implementation of the CBLJSONEncoding protocol:
- (id)encodeAsJSON

- (instancetype)initWithJSON:(id)jsonObject

When saving the nesting CBLModel subclass, encodeAsJSON worked as expected. But when reading, I can only get a __NSCFDictionary, not the NSObject<CBLJSONEncoding> one. And the break point for - (instancetype)initWithJSON:(id)jsonObject is not reached.

Following are the code I tought might be useful, I think I must had missed something:

The Nesting CBLModel

@interface NestingModel : CBLModel
@property (readwrite) NSArray *nestedObjectArray; //An array of the following NestedObject
@end

@implementation NestingModel
@dynamic nestedObjectArray
@end


The Nested NSObject <CBLJSONEncoding>

@interface NestedObject : NSObject <CBLJSONEncoding>
@property (readwrite) NSString  *nestedObjectName;
- (instancetype)initWithJSON:(id)jsonObject;
- (id)encodeAsJSON;
@end

@implementation NestedObject
- (id)initWithJSON:(id)jsonObject {
   
self = [self init];
   
if(self) {
       
self. nestedObjectName = jsonObject[@"nestedObjectName"];
   
}    
   
return self;
}

- (id)encodeAsJSON {
   
return @{@"nestedObjectName" : self. nestedObjectName};
}
@end


Nicolas Lapomarda

unread,
Mar 13, 2015, 6:02:59 AM3/13/15
to mobile-c...@googlegroups.com
You need to tell CBL that nestedObjectArray is an array of NestedObject objects.
You do that by adding this piece of code to your NestingModel implementation:

+ (Class)nestedObjectArrayItemClass
{
return [NestedObject class];
}

 Nico


--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/bdee77aa-092b-474c-b14a-92ae06f949b2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

SeanDev

unread,
Mar 13, 2015, 6:17:15 AM3/13/15
to mobile-c...@googlegroups.com
Thanks Nico for quick reply.

That solved my problem! Thanks again!

-Sean

Jens Alfke

unread,
Mar 13, 2015, 11:27:36 AM3/13/15
to mobile-c...@googlegroups.com
You haven’t declared what type of objects are in nestedObjectArray. All the model class can tell is that the property is an NSArray, but it doesn’t know what’s in it. By default it just decodes the JSON objects in it normally, so in your case you get NSDictionaries.

To declare what the objects in the array should get decoded as, you need to add a method to your implementation like this:
+ (Class) nestedObjectArrayItemClass {
return [NestedObject class];
}

(This should be described in the CBLModel documentation. If it isn’t, let me know and I’ll get it added.)

—Jens

lu...@lumobodytech.com

unread,
Sep 13, 2015, 3:39:17 PM9/13/15
to Couchbase Mobile
How can I tell the NestedObject class, to return [NestedObject class]? I mean how to call + (Class)nestedObjectArrayItemClass this method?

Jens Alfke

unread,
Sep 15, 2015, 2:42:22 PM9/15/15
to mobile-c...@googlegroups.com

> On Sep 13, 2015, at 10:38 AM, lu...@lumobodytech.com wrote:
>
> How can I tell the NestedObject class, to return [NestedObject class]? I mean how to call + (Class)nestedObjectArrayItemClass this method?

You don’t call +nestedObjectArrayItemClass. You just implement that method in your model subclass as a clue to tell CBLModel what class the array items should be. (It will call that method while getting the property value.)

—Jens
Reply all
Reply to author
Forward
0 new messages