Restkit:外键关系存根

Restkit: Stubs for foreign key relationships

在给定以下两个 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
}

团队-swift

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 就可以完成这项工作吗?

最后我计划让这个东西离线工作并在上网时同步。关于实施它的任何提示?

谢谢。

您似乎想要以下内容:

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)