我需要对核心数据执行大量的导入任务.
假设我的核心数据模型如下所示:
Car
----
identifier
type
Run Code Online (Sandbox Code Playgroud)
我从我的服务器获取汽车信息JSON列表,然后我想将它与我的核心数据Car对象同步,这意味着:
如果它是一辆新车 - > Car从新信息创建一个新的Core Data 对象.
如果汽车已经存在 - >更新Core Data Car对象.
所以我想在后台进行导入,而不会阻止UI,而使用滚动显示所有汽车的汽车表视图.
目前我正在做这样的事情:
// create background context
NSManagedObjectContext *bgContext = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[bgContext setParentContext:self.mainContext];
[bgContext performBlock:^{
NSArray *newCarsInfo = [self fetchNewCarInfoFromServer];
// import the new data to Core Data...
// I'm trying to do an efficient import here,
// with few fetches as I can, and in batches
for (... num of batches ...) {
// do batch import... …Run Code Online (Sandbox Code Playgroud) 我将数以万计的对象插入到我的Core Data实体中.我有一个NSManagedObjectContext,save()每次添加对象时我都会调用托管对象上下文.它可以工作,但在运行时,内存从大约27M增加到400M.即使在导入完成后它仍然保持在400M.
关于批量插入有很多SO问题,每个人都说阅读Efficiently Importing Data,但是它在Objective-C中,我很难在Swift中找到解决这个问题的真实例子.
我已经阅读了一些博客,但我仍然对如何使用NSPersistentContainer performBackgroundTask创建实体并保存它感到困惑.通过调用方便的方法创建一个实例后init(context moc: NSManagedObjectContext)的performBackgroundTask() { (moc) in }块,如果我检查container.viewContext.hasChanges这个返回false,并说没有什么保存的,如果我叫节省moc(该块所产生的背景MOC)我得到的错误是这样的:
Run Code Online (Sandbox Code Playgroud)fatal error: Failure to save context: Error Domain=NSCocoaErrorDomain Code=133020 "Could not merge changes." UserInfo={conflictList=( "NSMergeConflict (0x17466c500) for NSManagedObject (0x1702cd3c0) with objectID '0xd000000000100000 <x-coredata://3EE6E11B-1901-47B5-9931-3C95D6513974/Currency/p4>' with oldVersion = 1 and newVersion = 2 and old cached row = {id = 2; ... }fatal error: Failure to save context: Error Domain=NSCocoaErrorDomain Code=133020 "Could not merge changes." UserInfo={conflictList=( "NSMergeConflict (0x170664b80) for NSManagedObject (0x1742cb980) with objectID '0xd000000000100000 <x-coredata://3EE6E11B-1901-47B5-9931-3C95D6513974/Currency/p4>' …
我很擅长保存到coreData并使用iOS开发.
我想要实现的目标:
我希望能够在我的数据库中拥有一个具有唯一标识符/的idFB用户,并且该用户可以创建和检索他们的工作例程.
我走了多远?
我设法(我认为)创建一个方法,正确地routineName从Routine entity与右侧相关联的方法中检索User.看fetch方法.
我的问题:
我认为我没有与正确的实体关系协会保存User (usersExercise) <--->> Routine (userID).顺便说一下,我认为我的save方法不正确...因为我将整个用户保存到userID,它只是感觉不对?主要是因为当它吐出时它Routine.userID会拉动整个相关用户而不是特定ID?我真的不知道会发生什么
有谁可以帮我正确构建这些方法?我对coreData保存和建立正确关系的整个过程非常困惑.

- (void) save {
Routine *newRoutine = [NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:context];
newRoutine.users = [self getCurrentUser];
newRoutine.routineName = @"myRoutine Test Name";
NSError* error;
[context save:&error ];
NSLog(@"Saved now try to fetch");
[self fetch];
}
-(void) fetch {
NSFetchRequest *fetchRequestItems = [[NSFetchRequest alloc] init];
NSEntityDescription *entityItem = [NSEntityDescription entityForName:@"Routine" inManagedObjectContext:context];
[fetchRequestItems setEntity:entityItem];
User* user …Run Code Online (Sandbox Code Playgroud) core-data ×5
ios ×5
objective-c ×2
swift ×2
bulkinsert ×1
concurrency ×1
deprecated ×1
indexing ×1
iphone ×1
memory-leaks ×1
xcode ×1