下面显示了我添加到 NSManagedObject 的子类中的方法,用于填充单个实体并将其添加到 Core Data。我有大约 1000 个对象要添加到数据库中(我是在循环中进行的,而不是如下所示的列表)我的问题是关于性能,是否将 1000 个对象中的每一个逐个添加会增加大量开销到核心数据(我认为它确实如此)。有没有办法存储每个托管对象并以单一(更快)的方式添加它们。
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;
}