I am using RestKit release/0.10.0 branch for my application. I’m using CoreData and RestKit’s object mapping feature to load/synchronize data. The XML data I am loading looks as follows (simplified):
<iphoneSync>
<time>12-04-03 16:47</time>
<pages>
<page id="177">
<title>
Foo
</title>
<meta>
<tags>
<tag type="undef" id="16">Lorem</tag>
<tag type="undef" id="20">Ipsum</tag>
</tags>
</meta>
</page>
<page id="580">
<title>
Bar
</title>
<meta>
<tags>
<tag type="undef" id="16">Lorem</tag>
<tag type="undef" id="20">Ipsum</tag>
<tag type="" id="46">Abc</tag>
<tag type="symptom" id="47">Cde</tag>
</tags>
</meta>
</page>
</pages>
The domain classes Page and Tag look as follows:
@interface Page : NSManagedObject
@property (nonatomic, strong) NSNumber * id;
@property (nonatomic, strong) NSString * title;
@property (nonatomic, strong) NSSet *tags;
@end
@interface Tag : NSManagedObject
@property (nonatomic, strong) NSNumber * id;
@property (nonatomic, strong) NSString * name;
@property (nonatomic, strong) NSString * type;
@property (nonatomic, strong) NSSet *pages;
@end
The Page entity has many tags and a Tag can belong to multiple pages. So I have set the relationship from both sides to be To-many. The delete rule from Page to tags is cascade. From Tag to Pages it’s nullify.

The mapping setup:
//Tag mapping
RKManagedObjectMapping* tagMapping = [RKManagedObjectMapping mappingForClass:[Tag class] inManagedObjectStore:[RKManagedObjectStore defaultObjectStore]];
tagMapping.setDefaultValueForMissingAttributes = YES;
tagMapping.primaryKeyAttribute = @"id";
[tagMapping mapKeyPathsToAttributes:@"type",@"type",@"text",@"name",@"id",@"id", nil];
//Page mapping
RKManagedObjectMapping* pageMapping = [RKManagedObjectMapping mappingForClass:[Page class] inManagedObjectStore:[RKManagedObjectStore defaultObjectStore]];
pageMapping.primaryKeyAttribute = @"id";
[pageMapping mapKeyPathsToAttributes:@"id",@"id",@"title",@"title",nil];
[pageMapping mapKeyPath:@"meta.tags.tag" toRelationship:@"tags" withMapping:tagMapping];
pageMapping.setNilForMissingRelationships = YES;
pageMapping.setDefaultValueForMissingAttributes = YES;
//Root mapping
RKObjectMapping* rootMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[rootMapping mapKeyPath:@"iphoneSync.pages.page" toRelationship:@"pages" withMapping:pageMapping];
//RKObjectManager
RKManagedObjectStore* mainManagedObjectStore = [RKManagedObjectStore objectStoreWithStoreFilename:OBJECT_STORE_FILE];
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];
[RKObjectManager setSharedManager:objectManager];
objectManager.acceptMIMEType = RKMIMETypeXML;
objectManager.serializationMIMEType = RKMIMETypeXML;
objectManager.objectStore = mainManagedObjectStore;
objectManager.objectStore.cacheStrategy = [RKInMemoryManagedObjectCache new];
objectManager.client.cachePolicy = RKRequestCachePolicyNone;
[objectManager.mappingProvider addObjectMapping:rootMapping];
[objectManager.mappingProvider addObjectMapping:pageMapping];
[objectManager.mappingProvider addObjectMapping:tagMapping];
Then I load it like this:
[objectManager loadObjectsAtResourcePath:@"/" usingBlock:^(RKObjectLoader* loader) {
loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[NSMutableDictionary class]];
}];
The objects are loaded and mapped properly, but the tags with ID 16 and ID 20 are stored twice in the database (one for each page entity). But I want them to be distinct such that tags with the same ID are only stored once. What am I doing wrong?
This issue has been resolved. It was a bug in RestKit. See https://github.com/RestKit/RestKit/issues/661
Clone the latest version of the development branch to have a working version.