CBLModel Property as an Interface Rather Than a Class

70 views
Skip to first unread message

Scott Ahten

unread,
Mar 9, 2014, 4:49:29 PM3/9/14
to mobile-c...@googlegroups.com
I've run into a scenario where two CBLModel subclasses implement the same interface and I want to store a reference to either of these subclasses as a property on a third CBLModel subclass. 

The compeller doesn't complain, but I was unsure if CBLModel handle this scenario correctly. Note: both CBLModel classes whom implement the same interface have a type property and are registered with CBLModelFactory. 

Thanks, 

- Scott

Jens Alfke

unread,
Mar 9, 2014, 5:34:46 PM3/9/14
to mobile-c...@googlegroups.com

On Mar 9, 2014, at 1:49 PM, Scott Ahten <lightand...@gmail.com> wrote:

I've run into a scenario where two CBLModel subclasses implement the same interface and I want to store a reference to either of these subclasses as a property on a third CBLModel subclass. 

The compeller doesn't complain, but I was unsure if CBLModel handle this scenario correctly. Note: both CBLModel classes whom implement the same interface have a type property and are registered with CBLModelFactory. 

This will work fine. Internally, CBLModel calls the model-factory to resolve the docID into a model instance.

—Jens

Scott Ahten

unread,
Mar 9, 2014, 7:50:34 PM3/9/14
to mobile-c...@googlegroups.com


On Sunday, March 9, 2014 5:34:46 PM UTC-4, Jens Alfke wrote:


This will work fine. Internally, CBLModel calls the model-factory to resolve the docID into a model instance.

—Jens

Thanks for the quick turnaround, Jens! 

Scott Ahten

unread,
Mar 17, 2014, 7:17:39 PM3/17/14
to mobile-c...@googlegroups.com
I'm essentially trying to do the same thing, but with an array property on a CBLModel subclass.

So, rather than trying to define... 

@property (nonatomic, assign) id <MyProtocol> thingThatImplementsMyProtocol; 

I'm trying to define... 

@property (nonatomic, assign) NSArray *objectsThatImplementMyProtocol;

From what I understand, we implement the class method: + (Class) itemClassForArrayProperty: (NSString*)property to define the type of objects in the array. However, I'm not sure how this would work in the above case, as the method returns an Objective-C Class type rather than a protocol. 




  

Jens Alfke

unread,
Mar 17, 2014, 8:40:18 PM3/17/14
to mobile-c...@googlegroups.com

On Mar 17, 2014, at 4:17 PM, Scott Ahten <lightand...@gmail.com> wrote:

I'm trying to define... 
@property (nonatomic, assign) NSArray *objectsThatImplementMyProtocol;
From what I understand, we implement the class method: + (Class) itemClassForArrayProperty: (NSString*)property to define the type of objects in the array. However, I'm not sure how this would work in the above case, as the method returns an Objective-C Class type rather than a protocol. 

Hm, good point. You can look at the implementation in CBLModel to see what it’s doing; IIRC, the Class returned from itemClassForArrayProperty: is only used if the CBLModelFactory doesn’t know what class to use for that particular document. So as long as you’ve registered the document ‘type’ property (or whatever) with the factory, it should be OK.

—Jens

Scott Ahten

unread,
Mar 18, 2014, 3:36:38 PM3/18/14
to mobile-c...@googlegroups.com
On Monday, March 17, 2014 8:40:18 PM UTC-4, Jens Alfke wrote:

Hm, good point. You can look at the implementation in CBLModel to see what it’s doing; IIRC, the Class returned from itemClassForArrayProperty: is only used if the CBLModelFactory doesn’t know what class to use for that particular document. So as long as you’ve registered the document ‘type’ property (or whatever) with the factory, it should be OK.

—Jens

Thanks Jens. I'll take a look at the implementation.  
Reply all
Reply to author
Forward
0 new messages