iPhone Guy
iPhone Guy

Reputation: 1305

Relationship Mapping Restkit

I have two tables say Articles and Authors. Each Article have many authors and same author can be come in multiple Articles. My Json response is looks like this.

{
    "Articles": [
        {
            "artName": "ABC",
            "artId": "1",
            "Authors": [
                {
                    "autName": "James",
                    "autId": "200",
                    "email": "[email protected]"
                },
                {
                    "autName": "Mark",
                    "autId": "201",
                    "email": "[email protected]"
                },
                {
                    "autName": "Robert",
                    "autId": "202",
                    "email": "[email protected]"
                }
            ]
        },
        {
            "artName": "BCD",
            "artId": "2",
            "Authors": [
                {
                    "autName": "James",
                    "autId": "200",
                    "email": "[email protected]"
                },
                {
                    "autName": "Ben",
                    "autId": "204",
                    "email": "[email protected]"
                },
                {
                    "autName": "Rayon",
                    "autId": "205",
                    "email": "[email protected]"
                }
            ]
        }
    ]
}

Mapping:

 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]];

I have made a relationship for Articles and Authors. It is mapping correctly, but when i try to fetch the authors for "artName" ABC. i am getting only 2 authors. but when i try to fetch data for "artName" BCD. i am getting 3 authors. when checked in DB, relationship key for record "James" is overwritten by record of BCD.

How can i access all the authors for both the articles.

Note: I am using Restkit 0.20

Thank you

Upvotes: 0

Views: 277

Answers (1)

Wain
Wain

Reputation: 119031

The relationship between the 2 objects needs to be many to many or the new assignment will explicitly break the old assignment.

When you create your RKRelationshipMapping, ensure that you set the assignmentPolicy to RKUnionAssignmentPolicy. The default is RKSetAssignmentPolicy which could be removing the existing setting.

Upvotes: 1

Related Questions