使用NSPersistentStoreCoordinator的点? [英] Point of using NSPersistentStoreCoordinator?

查看:120
本文介绍了使用NSPersistentStoreCoordinator的点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在来自斯坦福193P 的Core数据讲座中,教师在iTunes上的课程,教师编码而不使用 NSPersistentStoreCoordinator 并使用 NSManagedObjectModel 加载它来创建一个示例项目。但是,在查看其他代码示例和iPhone开发中的Big Nerd Ranch书时,他们创建了一个 NSManagedObjectModel 和PersistentStoreCoordinator,并设置 NSManagedObjectContext 这样。

In the Core Data lecture from Stanford 193P iPhone course on iTunes, the instructor coded up a sample project with Core Data without using NSPersistentStoreCoordinator and loading it with a NSManagedObjectModel. But in looking at other code samples and the Big Nerd Ranch book on iPhone development, they are creating a NSManagedObjectModel and PersistentStoreCoordinator and setting up the NSManagedObjectContext that way.

我的问题是这样做的目的是什么,这两种方法的利弊是什么? >

My question is what is the purpose of doing it this way, and what are the pros and cons of both approaches?

推荐答案

我紧跟同一个系列讲座。此特定示例从Flickr中提取数据(摄影师和照片),并将其加载到CoreData中。在这个应用程序中并不需要使用CoreData,因为它需要在每次应用程序加载时从flickr获取新数据,因此不会持久保存。教授只是使用上一个演示中的flickr提取应用程序作为起点,因为学生已经熟悉它(允许他专注于解释CoreData)。然而,正如rickster所提到的,使用核心数据而不将上下文保存到磁盘有很大的好处。

I followed the same lecture series very closely. This particular example pulls data (Photographers and Photos) from Flickr and loads them into CoreData. It wasn't really necessary to use CoreData in this app since it needs to fetch new data from flickr on every application load, therefore there is no point in saving persistently. The prof was just using the flickr fetching app from the previous demo as a starting point since students were already familiar with it (allowing him to focus on explaining CoreData). However, as rickster mentioned, there are huge benefits to using core data without saving the context to disk.

正如Paul在演示之前的演讲中解释的,核心数据库(在iOS5中):

As Paul explained in the lecture before the demo, a core database can be created (in iOS5) either by:


  1. 在创建新项目时单击应用模板的使用核心数据。

  2. 使用UIManagedDocument

第一种方法背后的想法是Xcode会把一堆代码放在AppDelegate设置你的文档目录/持久存储协调器/和模型。然后它将管理对象CONTEXT传递到您的初始视图控制器(在公共API中应该有一个NSManagedObjectContext属性),从那里你可以传递上下文像一瓶啤酒,当你连接到其他视图控制器。传递上下文是访问核心数据库的正确过程。

The idea behind the first approach is that Xcode will put a bunch of code in AppDelegate to set up your documents directory/persistent store coordinator/and model. It will then pass the managed object CONTEXT to your initial view controller (which should have an NSManagedObjectContext property in the public API) and from there you can pass the context around like a bottle of beer when you segue to other viewcontrollers. Passing the context around is correct procedure for accessing the core database.

使用UIManagedDocument非常相似,除了你的AppDelegate被遗忘。您可以使用应用程序文档目录中的URL路径创建UIManagedDocument(也许在初始视图控制器中)(注意:您必须手动检查文件是否已经退出,存在但未打开或不存在)。然后你可以像上面一样使用这个文档的上下文。

Using UIManagedDocument is very similar, except your AppDelegate is left alone. You create a UIManagedDocument (maybe in your initial view controller) using a URL path from your app's document directory (Note: you have to manually check to see if the file already exits, exists but is not open, or does not exist). Then you can use this document's context in the same way as above.

另一个注意:在AppDelegate中创建一个指向你的上下文的指针是一个好主意,所以你可以显式地保存你的上下文(只有当它准备好!应用崩溃或终止。

Another Note: It's a good idea to create a pointer to your context in your AppDelegate so you are able to explicitly save your context (only when it's ready!) when the app crashes or terminates.

持久存储协调器是为您自动设置的,您可以使用它的persistentStoreOptions属性进行配置(确实,您需要为了保存持久化上下文),或者通过继承子类化UIManagedDocument并覆盖所需的方法。

The persistent store coordinator is set up automatically for you and you can configure it using it's persistentStoreOptions property (and indeed you will need to in order to save the context persistently), or by subclassing UIManagedDocument and overriding desired methods.

阅读UIManagedDocument文档中的概述
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIManagedDocument_Class/Reference/Reference .html

Read the overview in UIManagedDocument documentation http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIManagedDocument_Class/Reference/Reference.html

两种方法的工作方式相同,并为您提供相同的控制权和访问权限。使用UIManagedDocuments,您可以在多个sqlite文件中创建多个数据库,也可以等待创建/设置数据库,直到需要它。 使用核心数据选项为您提供一个单核心数据库,它设置在应用程序负载,允许您集中CoreData的东西围绕AppDelegate,节省编码时间,是一个快速应用程序的好。我喜欢UIManagedDocument。

Both methods work the same way and provide you with the same control and access. With UIManagedDocuments you can create multiple databases in multiple sqlite files, you can also wait to create / set up the database until it's needed. The "use core data" option provides you with a single core database which it sets up on application load, allows you to centralize CoreData stuff around AppDelegate, saves coding time and is good for a fast-track app. I like UIManagedDocument.

如果你启动你的应用程序没有核心数据选项检查,并希望将其添加到AppDelegate,只需创建一个新的项目核心数据检查和复制所有的代码到您的AppDelegate(应该只是3属性及其访问器,以及一个方便的方法来访问文档目录)。您将需要指向您的初始视图控制器,模型等。

If you started you app without the core data option checked and would like to add it to AppDelegate, just create a new project with core data checked and copy all the code to your AppDelegate (should just be 3 properties and their accessors as well as a convenience method to access the documents directory). You will need to point in to your initial view controller, model, etc..

更新:
只是想添加一个其他的方便。如果您的托管对象上下文存储在您的appDelegate中,您可以通过使用

UPDATE: Just wanted to add one other convenience. If your managed object context is stored in your appDelegate, you can access it anywhere in your app just by using

NSManagedObjectContext* context = [[(AppDelegate*) [UIApplication sharedApplication] delegate] myManagedObjectContext];

这不需要传递。

对于任何CoreData应用程序,如果您对模型进行任何更改,在再次构建之前,确保手动删除模拟器中的应用程序。否则,您将在下一个版本中遇到错误,因为它将使用旧文件。

For any CoreData app, if you make any changes to your model, MAKE SURE TO MANUALLY DELETE THE APP IN THE SIMULATOR before building again. Otherwise you will get an error on the next build since it will use the old file.

这篇关于使用NSPersistentStoreCoordinator的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆