Core Data is certainly speedy
Core Data is certainly speedy
- Subject: Core Data is certainly speedy
- From: Scott Ellsworth <email@hidden>
- Date: Thu, 18 Aug 2005 11:10:43 -0700
Hi, all.
I just had something work with far less pain than I anticipated, and
wanted to share.
As part of my data load, I build a toMany relationship from each of
10k entities to anywhere from a few to a few hundred other entities
of the same class. I then want to iterate over all of these
entities, and find the ones with an attribute within two of the max.
The following code runs in a couple of seconds for all 10k entities,
and it does it in 9 lines. (plus a few for a sort descriptor.)
- (NSArray *)biggestTradePartnersForWorld:(World *)world {
NSNumber * largestTradeForWorld = [world
valueForKeyPath:@"email@hiddenBetweenAlongRoute"];
if (largestTradeForWorld==nil || [largestTradeForWorld
floatValue]<3.0) return [NSArray array];
NSNumber * minTrade = [NSNumber numberWithFloat:fmax
([largestTradeForWorld floatValue]-2.0, 3.0)];
NSDictionary * dict = [NSDictionary
dictionaryWithObjectsAndKeys:world, @"WORLD", minTrade, @"MINIMUM",
nil];
NSFetchRequest * fetchRequest = [[self managedObjectModel]
fetchRequestFromTemplateWithName:@"tradeRelationshipsFromWorldGreaterTha
n" 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;
}
I am impressed - usually, performing a fetch a few thousand entities
is something to be feared, or at least looked at with respect.
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