2011-09-07 49 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); 
      } 
     }]; 
    }]; 
}