Re: Should I set Core Data dependent key in the primary's setter?
Re: Should I set Core Data dependent key in the primary's setter?
- Subject: Re: Should I set Core Data dependent key in the primary's setter?
- From: Scott Ellsworth <email@hidden>
- Date: Wed, 17 Aug 2005 17:06:08 -0700
On Aug 17, 2005, at 2:50 PM, Bill Cheeseman wrote:
on 2005-08-17 2:24 PM, Scott Ellsworth at email@hidden wrote:
I want the logPop attribute in my core data model to change
whenever the value of pop changes.
It's a little late in the day for me to think straight, but I'll
give this a
go. If I understand what you're about, all you need to do is:
1. Define the attribute in your managed object model and mark it
transient.
2. Implement your getter, and put all the calculations in the getter.
3. Forget about writing a setter.
The basic idea is that, any time Core Data tries to access the
transient attribute, it will call your getter, which will handle
the calculation.
Based on some experimentation, this does not work for a SQL store.
It appears that Core Data will not call the getter on the in-memory
objects when they have a transient attribute.
I added the tradeBetweenAlongRoute transient property, a float, to my
model, and marked it transient.
I added a sort descriptor on the key "tradeBetweenAlongRoute". This
descriptor works great when applied using sortedArrayUsingPredicate,
as the fetch is then in memory, but when I did this against a saved
SQL store, I got the error 'unknown keypath tradeBetweenAlongRoute'
Code:
NSDictionary * dict = [NSDictionary dictionaryWithObject:world
forKey:@"WORLD"];
NSFetchRequest * fetchRequest = [[self managedObjectModel]
fetchRequestFromTemplateWithName:@"tradeRelationshipsFromWorld"
substitutionVariables:dict];
NSSortDescriptor * tradeSorter=[[[NSSortDescriptor alloc]
initWithKey:@"tradeBetweenAlongRoute" ascending:NO] autorelease];
NSArray * sortDescriptors=[NSArray arrayWithObject:tradeSorter];
[fetchRequest setSortDescriptors:sortDescriptors];
Result:
2005-08-17 16:45:47.865 Astrogator[2181] unresolved keypath:
tradeBetweenAlongRoute
Were this to work, it would be a boon, I could avoid a bunch of
duplicate data in my store.
And the fact that it's defined as an attribute in the managed
object model means you can make use of all of Core Data's KVO stuff
as if it were a persistent value.
KVO would work, and I use that, on in memory objects. It appears to
not work with fetch requests. I believe this is because a kvo
request causes a fault, which brings the object into memory. Sans
that, it stays on disk, and sqllite tries to mess with it. Since it
is a transient attribute, sqlite has no idea what to do with it.
I am very willing to be proved wrong on this one, as it would make my
life a LOT easier.
Am I overlooking something?
Perhaps, or perhaps my own knowledge is wrong. I had thought that
transient attributes were not kept in the persistent store, and that
fetch requests in a SQL store would be carried out against the
persistent data, thus making transient attributes inappropriate for
much of anything in a SQL store.
Scott
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden