2

我正在为我的应用程序使用 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>

域类PageTag如下所示:

@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 到标签的删除规则是级联的。从标签到页面它是无效的。

CoreData 属性

映射设置:

//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 的标签只存储一次。我究竟做错了什么?

4

1 回答 1

0

此问题已得到解决。这是 RestKit 中的一个错误。请参阅https://github.com/RestKit/RestKit/issues/661 克隆最新版本的开发分支以获得工作版本。

于 2012-04-25T10:01:29.790 回答