我正在为我的应用程序使用 RestKit release/0.10.0 分支。我正在使用 CoreData 和 RestKit 的对象映射功能来加载/同步数据。我正在加载的 XML 数据如下所示(简化):
<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>
域类Page和Tag如下所示:
@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
Page 实体有很多标签,一个标签可以属于多个页面。所以我将双方的关系设置为多对多。从 Page 到标签的删除规则是级联的。从标签到页面它是无效的。
映射设置:
//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];
然后我像这样加载它:
[objectManager loadObjectsAtResourcePath:@"/" usingBlock:^(RKObjectLoader* loader) {
loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[NSMutableDictionary class]];
}];
对象已正确加载和映射,但 ID 16 和 ID 20 的标签在数据库中存储了两次(每个页面实体一个)。但我希望它们是不同的,这样具有相同 ID 的标签只存储一次。我究竟做错了什么?