关系映射Restkit

时间:2013-08-02 14:17:09

标签: ios core-data mapping relationship restkit-0.20

我有两张表说文章和作者。每篇文章都有很多作者和同一作者可以有多篇文章。我的Json响应看起来像这样。

{
    "Articles": [
        {
            "artName": "ABC",
            "artId": "1",
            "Authors": [
                {
                    "autName": "James",
                    "autId": "200",
                    "email": "james@xyz.com"
                },
                {
                    "autName": "Mark",
                    "autId": "201",
                    "email": "mark@xyz.com"
                },
                {
                    "autName": "Robert",
                    "autId": "202",
                    "email": "robert@xyz.com"
                }
            ]
        },
        {
            "artName": "BCD",
            "artId": "2",
            "Authors": [
                {
                    "autName": "James",
                    "autId": "200",
                    "email": "james@xyz.com"
                },
                {
                    "autName": "Ben",
                    "autId": "204",
                    "email": "ben@xyz.com"
                },
                {
                    "autName": "Rayon",
                    "autId": "205",
                    "email": "rayon@xyz.com"
                }
            ]
        }
    ]
}

映射:

 RKObjectManager *manager = [[RestKit sharedDataManager] objectManager];
    RKEntityMapping *ArticleMapping = [RKEntityMapping mappingForEntityForName:@"Articles" inManagedObjectStore:manager.managedObjectStore];
    ArticleMaping.identificationAttributes = @[@"artId"];
   ArticleMaping addAttributeMappingsFromDictionary:@{
     @"artId": @"artId",
  }];


  RKEntityMapping *AuthorMapping = [RKEntityMapping mappingForEntityForName:@"Authors" inManagedObjectStore:manager.managedObjectStore];
    AuthorMapping.identificationAttributes = @[@"autId"];

AuthorMapping addAttributeMappingsFromDictionary:@{
     @"autId": @"autId",
@"autName" : @"autName",
@"email" : @"email"

  }];


[ArticleMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Authors" toKeyPath:@"Authors" withMapping:AuthorMapping]];

我与文章和作者建立了关系。它正确映射,但是当我尝试为“artName”ABC获取作者时。我只有2位作者。但是当我尝试获取“artName”BCD的数据时。我有3位作者。当在DB中检入时,记录“James”的关系键被BCD记录覆盖。

如何访问这两篇文章的所有作者。

注意:我使用的是Restkit 0.20

谢谢

1 个答案:

答案 0 :(得分:1)

2个对象之间的关系需要多对多,否则新的赋值将明确地破坏旧的赋值。

创建RKRelationshipMapping时,请确保将assignmentPolicy设置为RKUnionAssignmentPolicy。默认值为RKSetAssignmentPolicy,可能会删除现有设置。