• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Core Data is certainly speedy
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Prev by Date: (BOOL)isVisible
  • Next by Date: Best way to display dictionary in a table
  • Previous by thread: (BOOL)isVisible
  • Next by thread: Best way to display dictionary in a table
  • Index(es):
    • Date
    • Thread