Checking for relationship existence in a CBLModel subclass causes model instance to be created

11 views
Skip to first unread message

Brendan Duddridge

unread,
Oct 6, 2016, 12:06:29 AM10/6/16
to Couchbase Mobile
Hi,

I've got a small bit of code that's checking to see if a relationship exists in my model. The leaf of the relationship is a CBLModel subclass.

It seems that when I do this check, Couchbase Lite (iOS 1.3.1) is creating a model object for the checked property. But I don't want it to do that since my code is checking to see if the model object exists or not. It shouldn't create one if it doesn't exist.

Here's the backtrace of what's happening:

0 -[TFForm awakeFromInitializer]
1 -[CBLModel initWithDocument:orDatabase:]
2 +[CBLModel modelForDocument:]
3 -[CBLModel modelWithDocID:forProperty:ofClass:]
4 -[CBLModel  getModelProperty:]
5 -[LinkedFormViewController numberOfRowsInTableView:]

So in my "numberOfRowsInTableView" method, I check for the existence of my relationship like this:

if (self.field.linkToForm) {

// do some stuff

} else {

// do some other stuff

}



The call to self.field.linkToForm is triggering the creation of the TFForm instance and calling my awakeFromInitializer method.

Have I setup the relationship wrong somehow? Or is this a bug? Or maybe I'm misunderstanding how it's supposed to work?

I would expect that if the linkToForm property of the field instance was nil then it wouldn't automatically create one for me.

I simply have the linkToForm property defined like this:

@property (nonatomic, weak) TFForm *linkToForm;


and it's defined as @dynamic in the @implementation section.

Thanks,

Brendan





Message has been deleted

Jens Alfke

unread,
Oct 6, 2016, 12:40:14 AM10/6/16
to mobile-c...@googlegroups.com

On Oct 5, 2016, at 9:06 PM, Brendan Duddridge <bren...@gmail.com> wrote:

The call to self.field.linkToForm is triggering the creation of the TFForm instance and calling my awakeFromInitializer method.

What’s the value of the JSON “linkToForm" property in the document?
- If it’s nil, then the Obj-C .linkToForm property should be nil.
- If it’s not nil, then it will instantiate (or reuse) a model on a document with that ID. (Yes, even if it doesn’t exist in the db yet.)

—Jens

Brendan Duddridge

unread,
Oct 6, 2016, 1:13:24 AM10/6/16
to Couchbase Mobile
What’s the value of the JSON “linkToForm" property in the document?
- If it’s nil, then the Obj-C .linkToForm property should be nil.
- If it’s not nil, then it will instantiate (or reuse) a model on a document with that ID. (Yes, even if it doesn’t exist in the db yet)

Ah ok, that makes sense.

I suppose it's possible in this case then that the value of the property is still in the underlying document but the destination object it was referring to had been deleted at some point in the past. I think it's just a referential integrity issue. That's probably why it was creating it. I first noticed this problem because I saw a bunch of log messages stating "deallocated blah object with unsaved properties" and then I wondered why there were all these new objects created automatically, but not saved.

I think I have a good theory of what's happening here.

Thanks for the nudge in the right direction Jens!
Reply all
Reply to author
Forward
0 new messages