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

瀏覽次數:11 次
跳到第一則未讀訊息

Brendan Duddridge

未讀,
2016年10月6日 凌晨12:06:292016/10/6
收件者: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





訊息已遭刪除

Jens Alfke

未讀,
2016年10月6日 凌晨12:40:142016/10/6
收件者: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

未讀,
2016年10月6日 凌晨1:13:242016/10/6
收件者: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!
回覆所有人
回覆作者
轉寄
0 則新訊息