具有指向对象的属性的核心数据实体
我不确定如何维护核心数据实体与创建实体并将其提交到数据库时实例化的一些对象之间的双向关系。
我有许多与实体具有一对一关系的子类 MKAnnotation 对象。每次我的 fetchedResultsController 执行新的提取时,我假设先前提取的结果被释放,并且提取的 NSManagedObjects 被重新映射到内存中。所以我的一对一关系就破裂了。如果我可以在核心数据中保存一个指向 MKAnnotation 对象的指针,那么问题就解决了一半(一个方向的关系)。这有道理吗?你会怎么做?
当应用程序重新启动时,我删除了所有核心数据内容,因此关系信息的长期持久性不是我所关心的。
I'm not sure how to maintain a bi-directional relationship between my core data entities and some objects that are instantiated when the entities are created and committed to the database.
I have many subclassed MKAnnotation objects with one-to-one relationships to the entities. Every time my fetchedResultsController executes a new fetch, I am assuming that the results from a previous fetch are released and the NSManagedObjects that are fetched are remapped in memory. So my one-to-one relationships are broken. If I can save a pointer to the MKAnnotation objects in core data, that would fix half of the problem (the relationship in one direction). Does this make sense? How would you do this?
I delete all of the core data content when the application is restarted, so long term persistence of the relationship information is not a concern that I have.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
混合指针和托管对象通常是徒劳的,因为 Core Data 有如此多的优化,以至于直接内存管理几乎是不可能的,例如对象可能会恢复为错误。
你确实以错误的方式处理这件事。 Core Data 主要不是一个持久性 API,它是一个数据建模 API,旨在提供模式-视图-控制器设计应用程序的完整模型层。因此,您可以使用它而无需保存任何内容。如果您正在使用 Core Data 并且拥有地图注释等数据,则应在 Core Data 中对注释进行建模。这样做会简化一切。
由于没有 MSAnnotation 类,而只有 MKAnnotation 协议,因此这种情况下最简单的解决方案是创建一个实现 MKAnnotation 协议的 NSManagedObject 类。您可以将诸如 CLLocationCooperative2D 之类的位置数据转换为 NSValues,或者更好的是,只需为它们创建属性即可。由于该类实现了协议,因此您可以将托管对象传递到任何可以传递任何协议对象的地方。
Mixing pointers and managed objects is usually futile because Core Data has so many optimizations in place that direct memory management is all but impossible e.g. an object may revert to a fault.
You're really going about this the wrong way. Core Data isn't primarily a persistence API, its a data modeling API intended to provide the complete model layer of a Mode-View-Controller design app. As such, you can use it without saving anything at all. If you are using Core Data and you have data such as map annotation, the annotation should be modeled in Core Data. Doing so will simplify everything.
Since there is no MSAnnotation class but merely a MKAnnotation protocol, the simplest solution in this case would be to create a NSManagedObject class that implements the MKAnnotation protocol. You can either convert location data like
CLLocationCoordinate2D
into NSValues or better yet, just make attributes for them. Since the class implements the protocol, you could pass the managed objects anywhere you would pass any protocol object.