Restkit多对多关系会导致重复的对象

时间:2012-04-05 07:26:50

标签: objective-c ios core-data mapping restkit

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

域类页面标记如下所示:

@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实体有很多标签,Tag可以属于多个页面。所以我把双方的关系定为To-many。从页面到标记的删除规则是级联的。从标签到页面,它无效。

CoreData properties

映射设置:

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

1 个答案:

答案 0 :(得分:0)

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