简体   繁体   English

是NSDocument和CoreData的可能组合,还是NSPersistentDocument的唯一方法?

[英]Is NSDocument and CoreData a possible combination, or is NSPersistentDocument the only way?

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. 我希望我的NSDocument子类代表个人的时间表。 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. 我想使用CoreData存储学生可以选择的所有课程。 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. 问题是,当我创建一个基于NSDocument的应用程序并检查use CoreData框时,而不是使其成为一个基于NSDocument的应用程序与CoreData工具,它使它成为一个基于NSPersistentDocument的应用程序。

I don't think this is the behaviour I want. 我不认为这是我想要的行为。 Is there a way to use CoreData, but still have an NSDocument-based application? 有没有办法使用CoreData,但仍然有一个基于NSDocument的应用程序? Or is NSPersistentDocument what I should be using after all? 或者NSPersistentDocument毕竟我应该使用什么? Am I misunderstanding the whole NS*Document business? 我是否误解了整个NS * Document业务? Do you have any advice for my application's structure? 您对我的应用程序结构有什么建议吗?

Thanks for any help! 谢谢你的帮助!

Yes, you can use Core Data without using NSPersistentDocument . 是的,您可以在不使用NSPersistentDocument情况下使用Core Data。 Just instantiate NSPersistentStoreCoordinator and NSManagedObjectContext directly. 只是直接实例化NSPersistentStoreCoordinatorNSManagedObjectContext Here's some code: how do you create a NSManagedObjectContext 这是一些代码: 如何创建NSManagedObjectContext

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. 如果要在多个NSDocument实例之间共享目录实例,并且不希望将目录与每个文档一起保留,这是一个很好的方法。 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. 你必须自己实现,使用键控归档器,或者自己编写一个plist。

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. 使用NSManagedObject表示学生日程安排,让NSPersistentDocument处理加载和保存上下文。 You get a lot of useful functionality, such as undo–redo, for free. 您可以免费获得许多有用的功能,例如undo-redo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM