NSDocument 和 CoreData 是可能的组合,还是 NSPersistentDocument 是唯一的方法?
我正在制作一个应用程序,为我学校的人们创建课程时间表。这是我对应用程序的粗略设计:
我希望我的 NSDocument 子类代表个人的时间表。这个想法是,他们打开一个文档,并且可以将池中的课程添加到他们的时间表中,然后保存、共享、打开等。因此时间表将存储在用户选择的外部文件中。
我想使用 CoreData 来存储学生可以选择的所有课程。这些不会随着时间表的创建和编辑而改变,而是可能仅在应用程序启动时检查课程信息的更新时改变。
这似乎是构建我的应用程序的逻辑方式。问题是,当我创建一个基于 NSDocument 的应用程序并选中 use CoreData
框时,它不是使其成为具有 CoreData 功能的基于 NSDocument 的应用程序,而是使其成为基于 NSPersistentDocument 的应用程序。
我认为这不是我想要的行为。有没有办法使用 CoreData,但仍然拥有基于 NSDocument 的应用程序?或者 NSPersistentDocument 到底是我应该使用的吗?我是否误解了整个 NS*Document 业务?您对我的应用程序结构有什么建议吗?
感谢您的帮助!
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:
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.
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.
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.
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?
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以在不使用
NSPersistentDocument
的情况下使用 Core Data。只需直接实例化NSPersistentStoreCoordinator
和NSManagedObjectContext
即可。这里有一些代码:如何创建 NSManagedObjectContext如果您想共享如果您不想将目录与每个文档一起保存,那么这是一个好方法。您的应用程序或应用程序委托可以负责加载课程目录,您的文档可以负责加载和保存单个学生的时间表。您必须自己实现这一点,例如使用带密钥的归档程序,或者自己编写一个 plist。
相反,如果您愿意,您可以进一步利用核心数据。使用
NSManagedObject
表示学生日程安排,并让NSPercientDocument
处理加载和保存上下文。您可以免费获得许多有用的功能,例如撤消-重做。Yes, you can use Core Data without using
NSPersistentDocument
. Just instantiateNSPersistentStoreCoordinator
andNSManagedObjectContext
directly. Here's some code: how do you create a NSManagedObjectContextIf 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.Instead, if you like, you can further leverage Core Data. Represent student schedules with
NSManagedObject
s and letNSPersistentDocument
handle loading and saving the contexts. You get a lot of useful functionality, such as undo–redo, for free.