- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
在 iOS 应用程序中,我想将 NSPersistentStoreCoordinator
与 NSIncrementalStore
子类一起使用,用于从 REST API 获取数据,但也与 SQLite 存储一起使用,保存到磁盘。但是,如果我将这两种类型的持久存储添加到我的协调器中,则在我的托管对象上下文中调用 save:
没有任何效果。如果我只添加一个持久存储,而不是我的 NSIcrementalStore
子类的类型,那么保存会按预期工作。
有什么办法可以实现这个功能吗?
最佳答案
根据我的经验,最好的解决方案是拥有多个托管对象上下文,每个上下文都有自己的模型。
但是,有一种方法可以完成您想要的:
// 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 ..
您还应该检查这些链接以更好地了解 Core Data 的工作原理:
Core Data Programming Guide - Persistent Store Coordinator
SO: Two persistent stores for one managed object context - possible?
SO: Can two managed object context share one single persistent store coordinator?
另请查看 TechZen 在第二个链接中关于配置的评论,并在此处阅读:
Core Data Programming Guide - Configurations
这里有一个很好的教程来管理两个对象上下文:
关于ios - NSPersistentStoreCoordinator 有两种类型的持久存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12321954/
我最近从 xcode 3.x 更新到 4.2,当我在 4.2 中运行应用程序时,我遇到了核心数据问题。我还更新到了 iOS 5,所以问题可能就在那里,我不太确定。 这些应用程序在 3.x 中运行良好,
我正在开发一个使用 Core Data 的 iPhone 应用程序。该应用程序调用 Web 服务,解析生成的 XML 文件,然后在我的应用程序中创建或修改 Core Data 对象。我已经处理了 We
每次我更改应用程序的核心数据模型时,它都会在下次启动时生成不可恢复的错误:“用于打开商店的模型与用于创建商店的模型不兼容”。 我发现避免这种情况的唯一可靠方法是手动删除应用程序并让 Xcode 重新安
在 iOS 应用程序中,我想将 NSPersistentStoreCoordinator 与 NSIncrementalStore 子类一起使用,用于从 REST API 获取数据,但也与 SQLit
我有一个名为 TestProject 的 xcode4 项目,我正在尝试将 CoreData 添加到其中。我添加了一个包含一些实体的数据模型(名为 TestDataModel),并为这些实体创建了 N
我目前已经为我的 iOS 应用程序的当前版本下载了我的崩溃日志。我看到了一条我无法理解的奇怪消息: [NSPersistentStoreCoordinator(_NSInternalMethods)
我目前正在使用 iCloud 和 CoreData 在我的应用程序中同步数据,因此每次触发通知时,我都会更新我的本地数据数组。我遇到的问题是我的数据集越来越大,我不想每次有新通知时都更新整个数据集。
我正在考虑使用两个 persistent store coordinator,PSC1 将仅读取 一个 sqlite DB,PSC2 将读取和写入 DB。有时 PSC1 会从 DB重新加载内容。 你觉
来自 Stanford 193P 的核心数据讲座iTunes 上的 iPhone 类(class),讲师在不使用 NSPersistentStoreCoordinator 的情况下使用 Core Da
我正在尝试在一个项目上使用 Core Data,该项目的代码改编自 iOS 编程:The Big Nerd Ranch Guide(第三版)。打开 SQLite 文件会导致异常,无论阅读多少文档或挖掘
创建persistentStoreCoordinator时,对addPersistentStoreWithType的调用崩溃。使用 Zombies 进行调试会发现释放错误 -[NSRelationsh
我有一个通过 UIManagedObjectDocument 使用核心数据的应用程序。我正在尝试使用加密核心数据 ( https://github.com/project-imas/encrypted
我知道其他开发者也经常问过这个问题。 我正在使用核心数据。 在上次运行代码时,它工作正常,但今天当我构建并运行应用程序时,我发现了此错误。 由于未捕获的异常“NSInvalidArgumentExce
我刚刚设置了一台使用 Xcode 进行开发的新机器,当我打开、构建并运行一个现有的核心数据项目(它在我的旧机器上仍然运行良好)时,我收到“无法创建 NSPersistentStoreCoordinat
由于我遇到的崩溃,我正试图找到一个官方答案,类似于: nil is not a legal NSPersistentStoreCoordinator for searching for entity
我花了很多时间让我的 Core Data 模型在开发中的 iOS 应用程序中高效工作。在大多数情况下,我的一切工作都很顺利,但前几天我偶然遇到了一个独特的错误加载我的数据。我刚开始在我的 iPhone
我已经用(任务)建立了一个模型,在我理解核心数据的过程中,我做到了这一点:向上下文添加三个项目: NSManagedObjectModel *m = [NSManagedObjectModel mer
我有一个运行完美的 coreData 数据模型文件。由于一些特殊要求,我删除旧的数据模型文件并创建了另一个具有完全相同实体的数据模型文件。与之前的数据模型相比,实体没有变化。我已将其作为不同 bund
我已阅读I keep on getting "save operation failure" after any change on my XCode Data Model 错误代码: This
我正在使用 MagicalRecord, 这就是我设置 coreData 堆栈的方式 - (BOOL)application:(UIApplication *)application didFinis
我是一名优秀的程序员,十分优秀!