Restkit:外键关系的存根
Posted
技术标签:
【中文标题】Restkit:外键关系的存根【英文标题】:Restkit: Stubs for foreign key relationships 【发布时间】:2015-09-25 07:28:28 【问题描述】:在给定以下两个 REST API 端点的情况下,我很难理解如何映射用户和团队对象。我一直在阅读我在 SO 上能找到的所有内容,最接近的 QA 是这个RestKit: Creating stubs for foreign key relationships,但我无法适应我的情况。
/用户
"success": 1,
"users": [
"success": 1,
"name": "Damjan",
"surname": "Mozetič",
"teams": [1, 2]
]
/团队
"success": 1,
"teams": [
"id": 1,
"title": "Team name",
"user": 1
]
User.swift
class User: NSManagedObject
@NSManaged var userId: NSNumber
@NSManaged var firstName: String
@NSManaged var lastName: String
@NSManaged var teams: NSSet?
@NSManaged var teamFKIds: NSNumber? // FK
团队快速
class Team: NSManagedObject
@NSManaged var teamId: NSNumber
@NSManaged var title: String
@NSManaged var user: User?
映射:
// User
let userMapping = RKEntityMapping(forEntityForName: "User", inManagedObjectStore: mos)
userMapping.identificationAttributes = ["userId"]
userMapping.addAttributeMappingsFromDictionary([
"id": "userId",
"name": "firstName",
"surname": "lastName",
"team": "teamName",
"teams": "teamFKIds"
])
objectManager.addResponseDescriptor(RKResponseDescriptor(mapping: userMapping, method: .GET, pathPattern: "users", keyPath: nil, statusCodes: RKStatusCodeIndexSetForClass(.Successful)))
// Team
let teamMapping = RKEntityMapping(forEntityForName: "Team", inManagedObjectStore: mos)
teamMapping.identificationAttributes = ["teamId"]
teamMapping.addAttributeMappingsFromDictionary([
"id": "teamId",
"title": "title"
])
objectManager.addResponseDescriptor(RKResponseDescriptor(mapping: teamMapping, method: .GET, pathPattern: "teams", keyPath: nil, statusCodes: RKStatusCodeIndexSetForClass(.Successful)))
userMapping.addConnectionForRelationship("teams", connectedBy: ["teamFKIds": "teamId"])
根据我一直在阅读的内容,为 FK 创建存根是可行的方法,就像这样,虽然我知道我已经搞砸了一点:
let profileStubMapping = RKEntityMapping(forEntityForName: "Profile", inManagedObjectStore: mos)
userStubMapping.addPropertyMapping(RKAttributeMapping(fromKeyPath: nil, toKeyPath: "teamFKIds"))
userStubMapping.identificationAttributes = ["userId"]
objectManager.addResponseDescriptor(RKResponseDescriptor(mapping: userStubMapping, method: .Any, pathPattern: "teams", keyPath: "userId", statusCodes: RKStatusCodeIndexSetForClass(.Successful)))
有人可以帮我了解如何正确设置吗?
另外,还有一个问题。最终我的计划是将整个图表检索到核心数据中。我如何实现这一目标?只需为“/users”和“/teams”调用 getObjectsAtPath 就可以完成这项工作吗?
最后,我计划让事情离线工作并在上线时同步。有什么实现的技巧吗?
谢谢。
【问题讨论】:
您是否总是首先获得用户,然后才是团队?看来您只需要通过零键路径映射来存根,就是这样...... @Wain 是的,我确实首先获得用户。你能告诉我如何通过 nil keypath 设置存根吗?这是我难以理解的部分。 【参考方案1】:看起来你想要一些类似的东西:
let userMapping = RKEntityMapping(forEntityForName: "User", inManagedObjectStore: mos)
userMapping.identificationAttributes = ["userId"]
userMapping.addAttributeMappingsFromDictionary([
"id": "userId",
"name": "firstName",
"surname": "lastName"
])
let teamStubMapping = RKEntityMapping(forEntityForName: "Team", inManagedObjectStore: mos)
teamStubMapping.addPropertyMapping(RKAttributeMapping(fromKeyPath: nil, toKeyPath: "teamId"))
teamStubMapping.identificationAttributes = ["teamId"]
userMapping.addRelationshipMapping(sourceKeyPath: "teams", mapping: teamStubMapping)
【讨论】:
以上是关于Restkit:外键关系的存根的主要内容,如果未能解决你的问题,请参考以下文章
Restkit:当外键设置为 null 时,Core Data 中的关系不会重置