SQL and XML stores behave differently with sort descriptor
SQL and XML stores behave differently with sort descriptor
- Subject: SQL and XML stores behave differently with sort descriptor
- From: Scott Ellsworth <email@hidden>
- Date: Thu, 11 Aug 2005 13:55:04 -0700
Hi, all.
I am getting an error with a SQL store that I am not with an XML
store, and I want to know whether the behavioral difference is a bug
worth filing.
I have a custom subclass of an entity in my Core Data object model.
It has a method that returns a computed property. That property is
not a part of the object model, but adding it as a transient
attribute did not make a difference. I wanted to do a fetch, ordered
by this attribute.
The implementation:
- (float)tradeBetweenAlongRoute{
return fmin([[[self destinationWorld] tradeRating] floatValue],
[self tradeMod]);
}
I then do a fetch, ordered by this attribute:
- (NSArray *)biggestTradePartnersForWorld:(World *)world {
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];
NSError * error = nil;
NSArray * tradeRelationships = [[self managedObjectContext]
executeFetchRequest:fetchRequest error:&error];
if (tradeRelationships == nil) NSLog(@"Error: %@", error);
return tradeRelationships;
}
If I load a data file into a brand new XML store, this gives me a
list of results.
If I saveAs a SQL store, it throws the error (while executing the
fetch request):
2005-08-11 13:21:34.610 Astrogator[11881] unresolved keypath:
tradeBetweenAlongRoute
The reason is clear - CD does not have the needed information to do
the orderBy on the server. The workaround was to do the sort post
fetch. The following works:
- (NSArray *)biggestTradePartnersForWorld:(World *)world {
NSDictionary * dict = [NSDictionary dictionaryWithObject:world
forKey:@"WORLD"];
NSFetchRequest * fetchRequest = [[self managedObjectModel]
fetchRequestFromTemplateWithName:@"tradeRelationshipsFromWorld"
substitutionVariables:dict];
NSError * error = nil;
NSArray * tradeRelationships = [[self managedObjectContext]
executeFetchRequest:fetchRequest error:&error];
if (tradeRelationships == nil) NSLog(@"Error: %@", error);
NSSortDescriptor * tradeSorter=[[[NSSortDescriptor alloc]
initWithKey:@"tradeBetweenAlongRoute" ascending:NO] autorelease];
NSArray * sortDescriptors=[NSArray arrayWithObject:tradeSorter];
tradeRelationships = [tradeRelationships
sortedArrayUsingDescriptors:sortDescriptors];
return tradeRelationships;
}
I would have expected the code to fail in the same way for both
stores. (I ended up surprised when I did a saveAs and the broken
code showed that it was broken.)
Is this worth filing?
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