问题描述
我正在制作一个应用程序,该应用程序可以为学校的人们创建课程时间表.这是我为该应用程序想到的粗略设计:
I'm making an application which creates course timetables for people at my school. This is the rough design I had in mind for the application:
我希望我的NSDocument子类代表一个人的时间表.他们的想法是,他们打开一个文档,并可以将课程表从池中添加到时间表中,然后进行保存,共享,打开等操作.因此,时间表将存储在用户选择的外部文件中.
I'd like my NSDocument subclass to represent an individual's timetable. The idea is that they open up a document, and can add courses from a pool to their timetable, then save, share, open, etc. So the timetable will be stored in an external file, chosen by the user.
我想使用CoreData存储学生可以选择的所有课程.这些不会随时间表的创建和编辑而改变,而是可能仅在应用程序启动时检查课程信息的更新时更改.
I'd like to use CoreData to store all the courses from which students can choose. These will not be altered with the creation and editing of timetables, but instead, likely only on launch of the application, when it checks for updates to the course info.
这似乎是构建我的应用程序的逻辑方法.问题是,当我创建基于NSDocument的应用程序并选中use CoreData
框时,而不是使它成为具有CoreData功能的基于NSDocument的应用程序,而是使它成为基于NSPersistentDocument的应用程序.
This seems to be the logical way to structure my app. The problem is, when I create an NSDocument-based application and check the use CoreData
box, instead of making it an NSDocument-based app with CoreData facilities, it makes it an NSPersistentDocument-based app.
我不认为这是我想要的行为.有没有一种使用CoreData的方法,但是仍然有一个基于NSDocument的应用程序?还是NSPersistentDocument到底应该使用什么?我是否误解了整个NS * Document业务?您对我的应用程序结构有什么建议吗?
I don't think this is the behaviour I want. Is there a way to use CoreData, but still have an NSDocument-based application? Or is NSPersistentDocument what I should be using after all? Am I misunderstanding the whole NS*Document business? Do you have any advice for my application's structure?
感谢您的帮助!
推荐答案
是的,您可以使用Core Data而不使用NSPersistentDocument
.只需直接实例化NSPersistentStoreCoordinator
和NSManagedObjectContext
.这是一些代码:如何创建NSManagedObjectContext
Yes, you can use Core Data without using NSPersistentDocument
. Just instantiate NSPersistentStoreCoordinator
and NSManagedObjectContext
directly. Here's some code: how do you create a NSManagedObjectContext
如果您要在多个NSDocument
实例之间共享目录的一个实例,并且不想与每个文档一起保留该目录,这是一个不错的方法.您的应用程序或应用程序代表可以负责加载课程目录,而您的文档可以负责加载和保存单个学生的时间表.您必须自己实现,例如使用键控存档器或自己编写plist.
If you want to share an instance of the catalog among multiple NSDocument
instances, and don't want to persist the catalog along with each document, this is a good way to go. Your application or app delegate can take care of loading the course catalog, and your document can take take care of loading and saving an individual student's timetable. You'll have to implement that yourself, using a keyed archiver, say, or by writing a plist yourself.
相反,如果您愿意,您可以进一步利用Core Data.用NSManagedObject
表示学生时间表,并让NSPersistentDocument
处理加载和保存上下文.您可以免费获得许多有用的功能,例如undo-redo.
Instead, if you like, you can further leverage Core Data. Represent student schedules with NSManagedObject
s and let NSPersistentDocument
handle loading and saving the contexts. You get a lot of useful functionality, such as undo–redo, for free.
这篇关于是NSDocument和CoreData可能的组合,还是NSPersistentDocument是唯一的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!