NSOperationサブクラスを使用して大量のデータセットをインポートし、次のように保存します。
 - (void)main
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSRunLoopCommonModes];
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
[moc setPersistentStoreCoordinator:[self persistentStoreCoordinator]];
[moc setUndoManager:nil]; //to make the import more effecient
NSError *error; 
for (NSManagedObject *taskInfo in self.tasks) { //self.tasks are the xml returned from a web service
 Task *taskDB  = [NSEntityDescription insertNewObjectForEntityForName:@"Task"inManagedObjectContext:moc];
        taskDB.taskID = [taskInfo valueForKey:@"TaskID"];
        taskDB.taskAssignedDate = [taskInfo valueForKey:@"TaskAssignDate"];
        taskDB.corporate = [self getCorporate:moc :[[taskInfo valueForKey:@"FacilityInfo"] valueForKey:@"ID"] ]; 
        taskDB.dateTime = [[NSDate date]retain];
        taskDB.requestNumber = [taskInfo valueForKey:@"RequestNumber"];
 ... //there are a lot of other properties for the task table
 } //for
 [moc save:&error];
 [moc reset];
 [pool drain], pool = nil;
 }
ただし、managedObjectContextループ内の最後のレコードのみを保存し、すべてのレコードを保存するわけではありません。ただし、保存コードをループ内に配置するmanagedObjectContextと、想定どおりにすべてのレコードが保存されます。また、ループ内にカウンターを設定して(10)レコード後に保存することで、いくつかのレコードの後に保存しようとしましたが、同じ問題が発生し、moc10ループ実行ごとに1レコードが保存されます。どうすればこの問題を解決できますか?すべてのレコードを一度に、または10回のループ実行ごとmocに保存したい。
よろしくお願いします。