从字典迁移到核心数据实体
我有一个数据模型,其中有一个 Person 实体,它有一个可转换属性,它是包含信息的字典数组。该模型比那个大得多,这只是我遇到麻烦的部分。它是一个老开发人员这样设计的,在接手这个项目时我需要将其迁移为100%核心数据。
因此,我需要做的是创建一个新实体,然后逐步遍历 Person 数组中的每个字典,并使用该字典中的信息创建该实体的新实例。我以为我可以使用 NSEntityMigrationPolicy 为这个新实体设置自定义迁移,但似乎核心数据迁移期望 X 个源实体转换为 X 个目标实体。因为我现在技术上有 0 个源实体(因为它们位于 Core Data 并不真正了解的数组中),所以我不确定如何使迁移在此过程中创建新实体。
实现我想要实现的目标的最佳方法是什么,或者更确切地说,在迁移过程中的哪个位置?我过去一直使用轻量级迁移,所以这是我第一次尝试自定义迁移。
I've got a data model where there is a Person entity, which has a transformable attribute which is an array of dictionaries containing information. The model is much bigger than that, this is just the part I'm having trouble with. It was designed this way by an old developer, and in taking over the project I need to migrate this to be 100% core data.
So what I need to do is create a new entity, then step through each dictionary in the Person's array and create new instances of that entity with the information from that dictionary. I thought I could use an NSEntityMigrationPolicy to set up a custom migration for this new Entity, but it seems the Core Data migration is expecting X number of source entities to translate to X number of destination entities. Because I technically have 0 source entities right now (because they're in an array that Core Data doesn't really know anything about), I'm not sure how I can make the migration create new entities during the process.
What, or rather where in the migration procedure, is the best way to do what I'm trying to accomplish? I've always used lightweight migration in the past, so this is my first adventure in custom migration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
了解您的数据模型(架构)会有所帮助 - 但我们假设您的 Person 实体现在拥有家庭住址和最喜欢的餐馆列表。我们进一步假设您将创建新实体 Address 和 Restaurant 以及以下关系:
Person 有一个 Address,因此从 Person 到 Address 存在一种名为“homeAddress”的一对一关系。地址与人员之间存在逆向多关系,因为许多人可以居住在同一地址。
人与餐厅之间存在一对多关系(称为餐厅)。 Restaurant 也可以与 Person 具有一对多关系(尽管这可能是双向性没有真正意义的情况之一)。
无论如何,现在的重点是 - 除了 PersonToPerson NSEntityMigrationPolicy 子类之外,您还将拥有 PersonToAddress 和 PersonToRestaurant。这些将是您解压旧数据并使用它来实例化和初始化新的 Address 和 Restaurant 对象的地方。
当然,还有很多其他复杂的问题。例如,您不希望为每个喜欢该餐厅的人创建同一餐厅的新实例。您将需要跟踪新创建的餐厅。
您将需要战略性地订购您的映射 - 可能首先使用 PersonToPerson。
您可能想查看 Marcus Zarra 的核心数据示例代码,甚至可能购买他的书。
It would help to have a sense of your data model (schema) - but let's assume that your Person entity now holds home address and list of favorite restaurants. And let's further assume that you will be creating new entities Address and Restaurant along with the following relationships:
Person has one Address, so there's a to-one relationship from Person to Address called "homeAddress". There's an inverse to-many relationship from Address to Person, because many people could live at the same address.
Person has a to-many relationship (called restaurants) to Restaurants. Restaurant could also has a to-many relationship to Person (though this might be one of those cases where bidirectionality doesn't really make sense).
Anyway, the point is that now - in addition to your PersonToPerson NSEntityMigrationPolicy subclass, you will also have PersonToAddress and PersonToRestaurant. These will be the places that you unpack the old data and use it to instantiate and initialize new Address and Restaurant objects.
Of course, there are lots of other complicating issues. For example, you won't want to be creating a new instance of the same Restaurant for every Person who likes it. You will need to keep track of newly created Restaurants.
You will want to order your mappings strategically - probably with PersonToPerson first.
You might want to look at Marcus Zarra's Core Data sample code and maybe even buy his book.