The following shows a method that I have added to a subclass of NSManagedObject to populate and add individual entities to Core Data. I have about a 1000 objects to add to the data base (which I am doing in a loop, not a list as shown below) My question is with regards to performance, does adding each of the 1000 objects 1 by one add a significant overhead to the Core Data (I would assume it does). Is there a way to store each of the managed objects and them add them in a single (faster) way.
NSManagedObjectContext *context = [[self managedDocument] managedObjectContext];
[Atomal createAtomalInContext:context withName:@"H11" age:@57 andType:@"Nantar"];
[Atomal createAtomalInContext:context withName:@"H23" age:@22 andType:@"Nantar"];
[Atomal createAtomalInContext:context withName:@"H54" age:@11 andType:@"Nantar"];
[Atomal createAtomalInContext:context withName:@"H34" age:@98 andType:@"Nantar"];
[Atomal createAtomalInContext:context withName:@"H17" age:@35 andType:@"Nantar"];
.
+ (Atomal *)createAtomalInContext:(NSManagedObjectContext *)context withName:(NSString *)name age:(NSNumber *)age andType:(NSString *)type {
Atomal *atomal = nil;
atomal = [NSEntityDescription insertNewObjectForEntityForName:@"Atomal" inManagedObjectContext:context];
// POPULATE PROPERTIES
[atomal setName:name];
[atomal setAge:age];
[atomal setType:type];
NSLog(@"CORE: Adding >>> %@ %@ %@", [atomal name], [atomal age], [atomal type]);
return atomal;
}
This should be fine. If you are really worried about performance and you are actually having issues with your implementation then you should profile and get real results to work on instead of just asking for opinions.
In saying that this should be fairly inexpensive as all of the managed object are created in memory – it will be when you call
save:that Core Data starts to hit the disk. The IO of hitting the disk is the slow part so if you make sure you keep that to a minimum you should be fine.Again one of the more expensive things you are doing here is logging, which is IO outputting to a log, you can really notice this effect if you do a lot of logging in a tight loop.