Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've gotten all this to work really well, thanks to Norman, who put me on the right path. I'll post my helper class here for others.</p> <p>Basically, my helper class will look up if an NSManagedObject exists for some ID, and can create it for some ID. This executes quickly enough for me, with 1,000 find/create operations taking around 2 seconds on my iPhone (I also did a few other things there, pure find/create is likely faster).</p> <p>It does this by caching a dictionary of all the NSManagedObjects, and checking that cache rather than executing a new NSFetchRequest. </p> <p>A couple of modifications that could help things speed up even further: 1. Get only selected properties for the NSManagedObjects 2. Only get the identifier property for the NSManagedObject into a dictionary, instead of the whole object. In my performance testing, the single query wasn't the slow part (but with only 1,000 items, I'd expect it to be fast). The slow part was the creation of the items.</p> <pre><code> #import "CoreDataUniquer.h" @implementation CoreDataUniquer //the identifying property is the field on the NSManagedObject that will be used to look up our custom identifier -(id)initWithEntityName:(NSString*)newEntityName andIdentifyingProperty:(NSString*)newIdProp { self = [super init]; if (self != nil) { entityName = [newEntityName retain]; identifyingProperty = [newIdProp retain]; } return self; } -(NSManagedObject*)findObjectForID:(NSString*)identifier { if(identifier == nil) { return nil; } if(!objectList) { NSManagedObjectContext *moc = [(AppDelegate*)[UIApplication sharedApplication].delegate managedObjectContext]; NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entityName inManagedObjectContext:moc]; NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; [request setEntity:entityDescription]; NSError *error; NSArray *array = [moc executeFetchRequest:request error:&amp;error]; objectList = [[NSMutableDictionary dictionary] retain]; for (NSManagedObject* p in array) { NSString* itemId = [p valueForKey:identifyingProperty]; [objectList setObject:p forKey:itemId]; } } NSManagedObject* returnedObject = [objectList objectForKey:identifier]; return returnedObject; } -(NSManagedObject*)createObjectForID:(NSString*)identifier { NSManagedObject* returnedObject = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:[(AppDelegate*)[UIApplication sharedApplication].delegate managedObjectContext]]; [returnedObject setValue:identifier forKey:identifyingProperty]; [objectList setObject:returnedObject forKey:identifier]; return returnedObject; } - (void) dealloc { DESTROY(entityName); DESTROY(identifyingProperty); [super dealloc]; } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload