【发布时间】:2020-04-21 10:23:18
【问题描述】:
我正在尝试对以下数据进行建模,以便将其存储在 CoreData 中。
由于它使用自定义类,我不确定如何正确建模。
每个Account 都有一个id 和一个profile
struct Account {
let id: String
let profile: UserProfile
}
每个配置文件都有自己的 id、name 以及 Post 数组
struct UserProfile {
let id: String
let name: String
let posts: [Post]
}
帖子由id、一个与个人资料上的ID匹配的authorId和一个title组成
struct Post {
let id: String
let authorId: String // this maps to the UserProfile id field
let title: String
}
一个帐户可以有 1 个个人资料,反之亦然。然而,一个配置文件可以有 N 个 Posts。
我假设我需要 3 个实体,Account、UserProfile 和 Post。
Account 和 UserProfile 将具有一对一的关系。
我不确定如何为 UserProfile 和 Post 建模。
UserProfile 是否应该与 Post 建立一对多的关系?
是否有可能 Post 与 UserProfile 具有一对一的关系,而 UserProfile 与 Post 具有一对多的关系?
另外,我假设 CoreData 不支持 Codable,所以 profile 属性不应该作为 Attribute 在 Account 上存在,而是作为 Relationships 字段中的值存在?
【问题讨论】:
标签: ios swift core-data codable