我是否应该创建 NSManagedObject 子类?

发布于 2024-10-10 20:17:54 字数 316 浏览 2 评论 0原文

我花了几天时间学习和编写 NSCoding 并终于让它工作了。然而,归档和取消归档(相当复杂的)对象图花费了很长时间,这是不可接受的。在网上搜索了一段时间后,我认为更好的方法是使用核心数据。

您是否建议 1) 我应该将所有类重写为 NSManagedObject 的子类,还是 2) 我应该在每个类中创建 NSManagedObject 的实例变量,以便对类进行的任何更改也会更新其核心数据表示?无论哪种方式都需要对现有类进行重大更改,并且我认为如果它改变了类的初始化方式,我也必须更新大量单元测试用例。

你有什么建议吗?我真的不想再次走上错误的道路......

谢谢!

I have spent a few days learning and writing NSCoding and finally got it working. However, it took very long to archive and unarchive the (quite complex) object graph, which is unacceptable. After searching the internet for some time, I think the better way is to use core data.

Do you recommend that 1) I should rewrite all my classes as subclasses of NSManagedObject or 2) should I create an instance variable of NSManagedObject in each of my class so that any changes to the class also updates its core data representation? Doing either way will need significant changes to the exiting classes and I think I have to update lots of unit test cases as well if it changes the way the classes are initialized.

What do you recommend? I really don't want to head to the wrong approach again...

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-10-17 20:17:54

如果您使用 Core Data,我会推荐 1)。

2)没有多大意义。例如,假设 A* a1A* a2 引用相同的 B* b。如果ABNSManagedObject的子类,则可以轻松地将这种关系保存到文件中,然后从文件中检索。但是,如果 ABNSManagedObject 实例作为 ivars,那么如何维护两个 A 引用的关系到一个B?您将被迫编写大量粘合代码,这些代码基本上由 Core Data API 提供。

如果您决定使用 Core Data,我可以给出的一个非常重要的建议是从头到尾仔细阅读 Apple 的文档非常非常,并克制住这种冲动从第一天开始就编写代码。 Core Data 是一套相当大的 API,在开始编写代码之前很好地掌握整个结构将为您节省很多时间。

I would recommend 1), if you use Core Data.

2) doesn't make much sense. For example, say A* a1 and A* a2 refer to the same B* b. If A and B are subclasses of NSManagedObject, this relationship can be easily saved to and then be retrieved from the file. But if A and B have NSManagedObject instances as ivars, how do you maintain this relationship that two As refer to one B? You will be forced to write a whole lot of glue codes, which are basically provided by the Core Data APIs.

If you decide to use Core Data, one very important advice I can give is to read Apple's documentations very, very carefully from the start to the finish, and to resist the urge to write codes from the day one. Core Data is a rather big set of API, and a good grasp of the whole structure before starting to write codes will save you many days afterwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文