Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSPersistentStoreCoordinator具有两种类型的持久存储?_Ios_Core Data - Fatal编程技术网

Ios NSPersistentStoreCoordinator具有两种类型的持久存储?

Ios NSPersistentStoreCoordinator具有两种类型的持久存储?,ios,core-data,Ios,Core Data,在iOS应用程序中,我想使用NSPersistentStoreCoordinator和NSIncrementalStore子类,从RESTAPI获取数据,还可以使用SQLite存储保存到磁盘。但是,如果我将这两种类型的持久性存储添加到我的协调器中,那么在我的托管对象上下文上调用save:就没有效果。如果我只添加了一个持久性存储,而不是我的NSIcrementalStore子类的类型,那么save将按预期工作 有没有办法实现此功能?根据我的经验,最好的解决方案是拥有多个托管对象上下文,每个上下文都

在iOS应用程序中,我想使用
NSPersistentStoreCoordinator
NSIncrementalStore
子类,从RESTAPI获取数据,还可以使用SQLite存储保存到磁盘。但是,如果我将这两种类型的持久性存储添加到我的协调器中,那么在我的托管对象上下文上调用
save:
就没有效果。如果我只添加了一个持久性存储,而不是我的
NSIcrementalStore
子类的类型,那么save将按预期工作


有没有办法实现此功能?

根据我的经验,最好的解决方案是拥有多个托管对象上下文,每个上下文都有自己的模型

但是,有一种方法可以实现您的目标:

// create the store coordinator
NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc] init];
// create the first store
NSPersistentStore *firstStore = [storeCoordinator addPersistentStoreWithType: NSIncrementalStore configuration:nil URL:urlToFirstStore options:optionsForFirstStore error:&error];
// now create the second one
NSPersistentStore *secondStore = [storeCoordinator addPersistentStoreWithType:NSSQLiteStore configuration:nil URL:urlToSecondStore options:optionsForSecondStore error:&error];

// Now you have two stores and one context
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:storeCoordinator];

// and you can assign your entities to different stores like this
NSManagedObject *someObject = [[NSManagedObject alloc] initWithEntity:someEntity insertIntoManagedObjectContext:context];
// here the relevant part
[context assignObject:someObject toPersistentStore:firstStore]; // or secondStore ..
您还应该查看这些链接,以更好地了解核心数据的工作原理:

另请查看TechZen在第二个配置链接中的评论,并在此处阅读:

下面是一个很好的教程,用于管理两个对象上下文:


谢谢,多亏了这些资源,我已经把一切都安排妥当了。然而,仍然存在一个问题:我有多个托管对象上下文,但只有一个具有两个持久存储的持久存储协调器。当我在我的主托管对象上下文上执行fetch请求时,我只希望它与我的SQLite持久存储相关联,而不使用我的NSIncrementalStore子类。如何实现这一点?看起来是
-[NSFetchRequest setAffectedStores::
@JordanKay不客气。很抱歉,我之前没有回答您关于取回请求的问题,这里阳光明媚,天气炎热,所以我在参加聚会:)。但看起来你做对了![NSFetchRequest setAffectedStores:]它是。