Caching variables in ManagedObjects
Caching variables in ManagedObjects
- Subject: Caching variables in ManagedObjects
- From: Alexander Griekspoor <email@hidden>
- Date: Thu, 23 Nov 2006 00:18:31 +0000
Hi,
I've been bug hunting for hours and *think* to have thought the cause
of many of my problems which started after I was adding caching of
variables to my custom managedobjects. I added code like:
- (NSString *)preferredName {
if(cachedPreferredName != nil){
cachedPreferredName = [[/* calculate my fancy name here */]retain];
}
return cachedPreferredName;
}
In the docs I found this method description:
- (void)didTurnIntoFault
Discussion
This method may be used to clear out custom data caches—transient
values declared as entity properties are typically already cleared
out by the time this method is invoked (see, for example,
refreshObject:mergeChanges:).
Which suggested me to add the following code:
- (void)didTurnIntoFault{
[cachedPreferredName release];
[super didTurnIntoFault];
}
However now I started to have strange crashes in kvoobservers etc.
Only now I realise what was going on. My mistakes were based on what
I think is my misinterpretation of how custom MO classes are used.
However I would like to hear if that's indeed true or whether I do
something wrong elsewhere.
I think the reason for the crashes is that I don't nil the pointer to
cachedPreferredName in the didTurnIntoFault message. I thought the MO
object would be deallocated upon faulting (and thus the caches
removed) and I would receive a new object the moment it would be
unfaulted again. However, it seems the object stays around all the
time, and because I didn't nil but did release I now have a problem
as the cache is not refreshed and refers to a released object.
Is this is indeed the case? Second, if so, wouldn't it make more
sense then to let the cache stay alive even after faulting
(eventually cleaning up in dealloc instead)? I presume you could even
avoid unnecessary faulting if you only hit the caches instead of
needing to access the store right? The only question would be: is it
likely/possible, that the faulted object has changed (and thus turned
the caches outdated) in the period the MO has been faulted?
I strongly have the feeling I start to get it but not fully grasp yet
the details of what is going on here, any help/advise on the topic
would be greatly appreciated!
Cheers,
Alex
***********************************
Mek (Alexander Griekspoor)
MekenTosj.com
Web: http://www.mekentosj.com
Mail: email@hidden
***********************************
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins (at) lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden