将 NSManagedObjectID 保存在 CoreData 中
我有一个 xcdatamodel,其中包含一组在静态库中构建的实体。
我将这个静态库包含在一个项目中。我想在主项目中创建另一个模型,其实体名为任务。我希望在实体中有一个属性,可以在其中存储在静态库中创建的实体的 NSManagedObjectID
。使用 NSManagedObjectID 我可以轻松获取主存储并获取实体。 最后可能有很多任务实体引用一个objectID。
是否可以 ?我还知道这听起来像一个关系模型,而 Core Data 不是,那么有没有更好的解决方案来处理这个主题?
谢谢
I have an xcdatamodel with a set of entities built in a static library.
I am including this static library in a project. I would like to create another model in main project, with entity named Task. I would like to have an attribute in the entity where I could store the NSManagedObjectID
of an entity created in static library. With NSManagedObjectID
I could easily fetch the main store and get the entity.
In the end there could be many Task entities refer to an objectID.
Is it possible ? I also understand that this is sounds like a relational model, which Core Data isn't, so is there a better solution for dealing with the subject?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSManagedObjectID
本身不符合编码。但您可以获取对象 ID 的 URL 表示形式并将其存储。像这样:然后,为了获取对象 ID 以检索它稍后引用的任务对象(
psc
是您的NSPersistentStoreCoordinator
,其中Task< /code> 实体处于活动状态):
确保绝不对临时托管对象 ID 执行此操作。
The
NSManagedObjectID
is in itself not coding compliant. But you can get the URL representation of an object ID and store that. Like this:Then in order to get the object ID back to retrieve the task object it refers to later (
psc
is yourNSPersistentStoreCoordinator
where theTask
entities live):Make sure to never do this to a temporary managed object ID.
您可以将托管对象 ID 作为 NSURL 存储在可转换属性中。
You can store a managed object ID as a NSURL in a transformable attribute.