小编asl*_*kjd的帖子

如何加快使用Core Data将新对象插入实体的速度

这是我的代码,它采用一些JSON数据并将其插入Core Data room实体:

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]);
        }
    }
Run Code Online (Sandbox Code Playgroud)

插入约900个物体时速度极慢.有没有办法让这个更高效和/或加快速度?

谢谢!

iphone core-data objective-c ios

2
推荐指数
1
解决办法
1000
查看次数

标签 统计

core-data ×1

ios ×1

iphone ×1

objective-c ×1