在 CoreData 中使用带有子模型的模型
Using models with sub models in CoreData
我正在尝试使用 CoreData
实现缓存。
到目前为止,我一直在存储简单的模型,但是我下面有一个模型包含 CodablePartialUser
和 CodableFeedItemType
.
等数据类型
这些类型应该如何在 CoreData 中建模?
我应该使用 Data
类型并以数据格式存储它们吗?
由于 CodableFeedItemType
是一个枚举,我是否应该存储原始值并再次在格式之间转换?
struct CodablePartialUser: Equatable, Codable {
let userID: String
let firstName: String
let lastName: String
init(userID: String, firstName: String, lastName: String) {
self.userID = userID
self.firstName = firstName
self.lastName = lastName
}
}
enum CodableFeedItemType: String, Codable {
case recognition = "RECOGNITION"
case news = "COMPANY_NEWS"
}
struct CodableFeedItem: Codable {
let id: String
let type: CodableFeedItemType
let createdDate: Date
let createdBy: CodablePartialUser
let likesCount: Int
let commentsCount: Int
let externalID: String
let title: String?
let imageURL: URL?
init(id: String, type: CodableFeedItemType, createdDate: Date, createdBy: CodablePartialUser, likesCount: Int, commentsCount: Int, externalID: String, title: String?, imageURL: URL?) {
self.id = id
self.type = type
self.createdDate = createdDate
self.createdBy = createdBy
self.likesCount = likesCount
self.commentsCount = commentsCount
self.externalID = externalID
self.title = title
self.imageURL = imageURL
}
}
对于 CodablePartialUser,您可以通过创建一个名为 "CodablePartialUser"
的新实体来使用关系
对于 CodableFeedItemType,您可以像这样使用枚举
enum CodableFeedItemType: String, Codable {
case recognition = "RECOGNITION"
case news = "COMPANY_NEWS"
}
extension CodableFeedItemEntity {
var type: CodableFeedItemType {
get {
return CodableFeedItemType(rawValue: typeRaw)!
}
set {
typeRaw = newValue.rawValue
}
}
}
我正在尝试使用 CoreData
实现缓存。
到目前为止,我一直在存储简单的模型,但是我下面有一个模型包含 CodablePartialUser
和 CodableFeedItemType
.
这些类型应该如何在 CoreData 中建模?
我应该使用 Data
类型并以数据格式存储它们吗?
由于 CodableFeedItemType
是一个枚举,我是否应该存储原始值并再次在格式之间转换?
struct CodablePartialUser: Equatable, Codable {
let userID: String
let firstName: String
let lastName: String
init(userID: String, firstName: String, lastName: String) {
self.userID = userID
self.firstName = firstName
self.lastName = lastName
}
}
enum CodableFeedItemType: String, Codable {
case recognition = "RECOGNITION"
case news = "COMPANY_NEWS"
}
struct CodableFeedItem: Codable {
let id: String
let type: CodableFeedItemType
let createdDate: Date
let createdBy: CodablePartialUser
let likesCount: Int
let commentsCount: Int
let externalID: String
let title: String?
let imageURL: URL?
init(id: String, type: CodableFeedItemType, createdDate: Date, createdBy: CodablePartialUser, likesCount: Int, commentsCount: Int, externalID: String, title: String?, imageURL: URL?) {
self.id = id
self.type = type
self.createdDate = createdDate
self.createdBy = createdBy
self.likesCount = likesCount
self.commentsCount = commentsCount
self.externalID = externalID
self.title = title
self.imageURL = imageURL
}
}
对于 CodablePartialUser,您可以通过创建一个名为 "CodablePartialUser"
的新实体来使用关系
对于 CodableFeedItemType,您可以像这样使用枚举
enum CodableFeedItemType: String, Codable { case recognition = "RECOGNITION" case news = "COMPANY_NEWS" } extension CodableFeedItemEntity { var type: CodableFeedItemType { get { return CodableFeedItemType(rawValue: typeRaw)! } set { typeRaw = newValue.rawValue } } }