Restkit:外键关系的存根

时间:2015-09-25 07:28:28

标签: swift mapping restkit foreign-key-relationship restkit-0.24.x

我很难理解如何在给定下面两个REST API端点的情况下映射用户和团队对象。我一直在阅读所有我能找到的东西,最接近的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)))

有人可以帮我理解如何正确设置吗?

另外,另一个问题。最终我的计划是将整个图表检索到Core Data中。我该如何实现这一目标?只需为“/ users”和“/ teams”调用getObjectsAtPath即可完成工作吗?

最后,我计划havnig离线工作并在上线时同步。有关实施它的任何提示吗?

谢谢。

1 个答案:

答案 0 :(得分: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)