0

我有两张表说文章和作者。每篇文章有很多作者,同一作者可以出现在多篇文章中。我的 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

谢谢

4

1 回答 1

1

2 个对象之间的关系需要是多对多的,否则新分配将显式破坏旧分配。

创建 时RKRelationshipMapping,请确保将 设置assignmentPolicyRKUnionAssignmentPolicy。默认设置是RKSetAssignmentPolicy删除现有设置。

于 2013-08-02T14:24:58.703 回答