如何使用 RKObjectMapping 在嵌套的 RLMObject 中设置相同的主键?
How can I set same primary key in nested RLMObject with RKObjectMapping?
我想在嵌套的 RLMObject 中设置相同的主键。
如何使用 RKObjectMapping 设置相同的 id?
首先,我制作了以下 json 文件。
JSON 文件
{
"id": 1
"obj2": {
"name": "name1"
}
}
其次,我制作了以下 swift class 文件。
Class 文件
class Obj2: RLMObject {
dynamic var id: Int <-- I want to set same id as Obj1.id
dynamic var name: String!
override class func primaryKey() -> String {
return "id"
}
class func responseMapping() -> RKObjectMapping {
let objectMapping = RKObjectMapping(forClass: self)
objectMapping.addAttributeMappingsFromDictionary([
"name": "name"
return objectMapping
}
}
class Obj1: RLMObject {
dynamic var id: Int!
dynamic var obj2: Obj2?
override class func primaryKey() -> String {
return "id"
}
class func responseMapping() -> RKObjectMapping {
let objectMapping = RKObjectMapping(forClass: self)
objectMapping.addAttributeMappingsFromDictionary([
"id": "id"
objectMapping.addPropertyMappingsFromArray([
RKRelationshipMapping(fromKeyPath: "obj2", toKeyPath: "obj2", withMapping: Obj2.responseMapping())
])
return objectMapping
}
}
您需要在同一个 JSON 基础对象上设置映射,而不是遍历范围,这会使映射依赖于关系。
class Obj2: RLMObject {
dynamic var id: Int
dynamic var name: String!
override class func primaryKey() -> String {
return "id"
}
class func responseMappingForBeingOwnedByObj1() -> RKObjectMapping {
let objectMapping = RKObjectMapping(forClass: self)
objectMapping.addAttributeMappingsFromDictionary([
"id": "id",
"name": "obj2.name"
])
return objectMapping
}
}
class Obj1: RLMObject {
dynamic var id: Int!
dynamic var obj2: Obj2?
override class func primaryKey() -> String {
return "id"
}
class func responseMapping() -> RKObjectMapping {
let objectMapping = RKObjectMapping(forClass: self)
objectMapping.addAttributeMappingsFromDictionary([
"id": "id"
])
objectMapping.addPropertyMappingsFromArray([
RKRelationshipMapping(fromKeyPath: ".", toKeyPath: "obj2", withMapping: Obj2.responseMappingForBeingOwnedByObj1())
])
return objectMapping
}
}
最后的警告:RestKit 目前并不真正支持 Realm。因此,您现在最好使用其他东西,例如Realm 对解码对象表示的有限内置支持,Realm+JSON or Decodable。
我想在嵌套的 RLMObject 中设置相同的主键。
如何使用 RKObjectMapping 设置相同的 id?
首先,我制作了以下 json 文件。
JSON 文件
{
"id": 1
"obj2": {
"name": "name1"
}
}
其次,我制作了以下 swift class 文件。
Class 文件
class Obj2: RLMObject {
dynamic var id: Int <-- I want to set same id as Obj1.id
dynamic var name: String!
override class func primaryKey() -> String {
return "id"
}
class func responseMapping() -> RKObjectMapping {
let objectMapping = RKObjectMapping(forClass: self)
objectMapping.addAttributeMappingsFromDictionary([
"name": "name"
return objectMapping
}
}
class Obj1: RLMObject {
dynamic var id: Int!
dynamic var obj2: Obj2?
override class func primaryKey() -> String {
return "id"
}
class func responseMapping() -> RKObjectMapping {
let objectMapping = RKObjectMapping(forClass: self)
objectMapping.addAttributeMappingsFromDictionary([
"id": "id"
objectMapping.addPropertyMappingsFromArray([
RKRelationshipMapping(fromKeyPath: "obj2", toKeyPath: "obj2", withMapping: Obj2.responseMapping())
])
return objectMapping
}
}
您需要在同一个 JSON 基础对象上设置映射,而不是遍历范围,这会使映射依赖于关系。
class Obj2: RLMObject {
dynamic var id: Int
dynamic var name: String!
override class func primaryKey() -> String {
return "id"
}
class func responseMappingForBeingOwnedByObj1() -> RKObjectMapping {
let objectMapping = RKObjectMapping(forClass: self)
objectMapping.addAttributeMappingsFromDictionary([
"id": "id",
"name": "obj2.name"
])
return objectMapping
}
}
class Obj1: RLMObject {
dynamic var id: Int!
dynamic var obj2: Obj2?
override class func primaryKey() -> String {
return "id"
}
class func responseMapping() -> RKObjectMapping {
let objectMapping = RKObjectMapping(forClass: self)
objectMapping.addAttributeMappingsFromDictionary([
"id": "id"
])
objectMapping.addPropertyMappingsFromArray([
RKRelationshipMapping(fromKeyPath: ".", toKeyPath: "obj2", withMapping: Obj2.responseMappingForBeingOwnedByObj1())
])
return objectMapping
}
}
最后的警告:RestKit 目前并不真正支持 Realm。因此,您现在最好使用其他东西,例如Realm 对解码对象表示的有限内置支持,Realm+JSON or Decodable。