小编mtS*_*ack的帖子

核心数据插入多个对象

这是用关系保存多个对象的正确方法吗?或者有没有办法改进代码并保存上下文一次?谢谢!!

for (NSDictionary *entries in dataArray){
    module = [NSEntityDescription insertNewObjectForEntityForName:@"Modules" inManagedObjectContext:context];
    module.m_id=[entries objectForKey:@"id"];
    module.m_name = [entries objectForKey:@"name"];
    module.m_timestamp = [NSDate date];

    //This line links the product by adding an entry to the NSSet of list for the module relation
    [product addModulesObject:module];

    //This line link the module with the product using product relation
    [module setProduct:product];

    NSError *error = nil;
    if (![context save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
}
Run Code Online (Sandbox Code Playgroud)

core-data insert object objective-c

5
推荐指数
1
解决办法
2791
查看次数

标签 统计

core-data ×1

insert ×1

object ×1

objective-c ×1