Re: Core Data: following a relationship to set a transient attribute during awakeFromFetch
Re: Core Data: following a relationship to set a transient attribute during awakeFromFetch
- Subject: Re: Core Data: following a relationship to set a transient attribute during awakeFromFetch
- From: Quincey Morris <email@hidden>
- Date: Mon, 15 Dec 2008 00:51:05 -0800
On Dec 14, 2008, at 18:25, Steve Mykytyn wrote:
I am using transient attributes as a nice and efficient way to
display formatted data (with line breaks) in an NSTableView, and am
running into trouble in my awakeFromFetch: method for a subclass of
NSManagedObject.
This works fine in the awakeFromFetch: when building a transient
attribute based on permanent attributes of the object in question, but
I'm trying to follow relationships to retrieve attributes from
several other NSManagedObjects.
The relationship is to the correct NSManagedObject, which is a
fault, as seen here:
12/14/08 5:50:43 PM myApp[14070] after we get it city =
<NSManagedObject: 0x150e4370> (entity: destination_city; id:
0x150d14e0 <x-coredata://E77970B1-4F89-4C1C-B788-334AEEF886EB/destination_city/p22919
> ; data: <fault>)
Trying to access attributes of the relation just fails with a
message like:
12/14/08 5:51:19 PM myApp[14070] *** NSRunLoop ignoring exception
'statement is still active' that raised during posting of delayed
perform with target 0x14c90c80 and selector 'invokeWithTarget:'
Sample code:
awakeFromFetch: in my NSManagedObject subclass
NSManagedObject *city = [self valueForKey:@"city"];
NSManagedObject *state = [self primitiveValueForKey:@"state"];
NSManagedObject *country = [self primitiveValueForKey:@"country"];
// ********* fails on the next statement - city is there, and
marked as a default...
NSString *cityName = [city valueForKey:@"city_name"];
NSString *stateName = [state valueForKey:@"state_name"];
NSString *countryName = [country valueForKey:@"countryName"];
NSString *transient_location = [NSString stringWithFormat:@"%@, %@\n
%@", cityName, stateName, countryName];
displayPatternValue is much slower than this approach and I don't
want to denormalize my tables unless I have to do so.
Suggestions?
Core Data appears to be telling you that it can't fault in the other
objects during awakeFromFetch. The neatest alternative is to implement
the string formatting in the transient attribute's getter (you don't
say what is the key of this attribute, so I'll assume "location"):
+ (NSSet*) keyPathsForValuesAffectingLocation {
return [NSSet setWithObjects: @"city", "city.city_name", @"state",
@"state.state_name", @"country", @"country.countryName", nil];
}
- (NSString*) location {
NSManagedObject *city = [self valueForKey:@"city"];
NSManagedObject *state = [self valueForKey:@"state"];
NSManagedObject *country = [self valueForKey:@"country"];
NSString *cityName = [city valueForKey:@"city_name"];
NSString *stateName = [state valueForKey:@"state_name"];
NSString *countryName = [country valueForKey:@"countryName"];
return [NSString stringWithFormat:@"%@, %@\n%@", cityName, stateName,
countryName];
}
Note that this *appears* to be less efficient, but even if it is, it's
unlikely to be a performance bottleneck.
It has the advantage of returning the correct result even if the city,
state or country change.
It also has the advantage of being KVO-compliant for the key
"location" with no additional housekeeping code.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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