2011-09-07 46 views
2

这里是我的代码,它正在采取一些JSON数据,并将其插入到核心数据房间实体:如何加快插入新的对象为实体与核心数据

for (NSDictionary *room in rooms) 
    { 
     NSDictionary *thisroom = [room objectForKey:@"room"]; 
     NSString *roomidstring = [thisroom objectForKey:@"roomid"]; 
     int roomid = [roomidstring intValue]; 
     NSString *roomname = [thisroom objectForKey:@"roomname"]; 
     NSString *buildingidstring = [thisroom objectForKey:@"buildingid"]; 
     int buildingid = [buildingidstring intValue]; 

     // import into database 
     NSManagedObject *roomInfo = [NSEntityDescription insertNewObjectForEntityForName:@"room" inManagedObjectContext:context]; 
     [roomInfo setValue:[NSNumber numberWithInteger:roomid] forKey:@"roomid"]; 
     [roomInfo setValue:roomname forKey:@"roomname"]; 
     [roomInfo setValue:[NSNumber numberWithInteger:buildingid] forKey:@"buildingid"]; 
     if (![context save:&error]) { 
      NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]); 
     } 
    } 

插入约900时,这是令人难以置信的慢对象。有什么办法可以提高效率和/或加快速度吗?

谢谢!

回答

8

是的,在完成循环之前不要保存,或者如果内存不足,则可以批量保存。保存操作非常昂贵,所以你应该避免经常像这样的紧密循环中保存。

for (NSDictionary *room in rooms) 
{ 
    NSDictionary *thisroom = [room objectForKey:@"room"]; 
    NSString *roomidstring = [thisroom objectForKey:@"roomid"]; 
    int roomid = [roomidstring intValue]; 
    NSString *roomname = [thisroom objectForKey:@"roomname"]; 
    NSString *buildingidstring = [thisroom objectForKey:@"buildingid"]; 
    int buildingid = [buildingidstring intValue]; 

    // import into database 
    NSManagedObject *roomInfo = [NSEntityDescription insertNewObjectForEntityForName:@"room" inManagedObjectContext:context]; 
    [roomInfo setValue:[NSNumber numberWithInteger:roomid] forKey:@"roomid"]; 
    [roomInfo setValue:roomname forKey:@"roomname"]; 
    [roomInfo setValue:[NSNumber numberWithInteger:buildingid] forKey:@"buildingid"]; 
} 


if (![context save:&error]) { 
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]); 
} 
+0

WOW,就像白天和黑夜。完成时间约为15到20秒。谢谢! – asljkdfkjd

1

如何将[context save]调用移出循环并将其全部保存在一个操作中?看来每次做一次保存会让事情变得更慢。

0

导入一次,将存储文件复制到文档目录,更新持久存储协调器代码以从新位置加载该文件。

0

另外(乔的回答),您还可以通过执行保存在另一个线程(NSPrivateQueueConcurrencyType)加快节省时间:

- (void)openDataBase { 
    // Set up _persistentStoreCoordinator accordingly. 

    privateWriterContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; 
    [privateWriterContext setPersistentStoreCoordinator:_persistentStoreCoordinator]; 

    context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; 
    context.parentContext = _privateWriterContext; 
} 

- (void)save 
{ 
    [context performBlock:^{ 
     NSError *error; 
     if (![context save:&error]) { // This will forward save to the parent, which is privateWriterContext. 
      NSLog(@"Error saving main DB: %@, %@", error, [error userInfo]); 
      NSAssert(false, nil); 
     } 
     [privateWriterContext performBlock:^{ 
      NSError *error; 
      if (![privateWriterContext save:&error]) { 
       NSLog(@"Error saving writer DB: %@, %@", error, [error userInfo]); 
       NSAssert(false, nil); 
      } 
     }]; 
    }]; 
}