在RestKit映射对象和引用中苦苦挣扎

时间:2012-10-03 17:47:03

标签: iphone objective-c core-data restkit relationship

这是我的实体

- HomeArticle
identifier
article (relationship)

- Article
identifier
title
subtitle
text
home (relationship)

我的有效负载如下所示:

[
    {
        "id": "1",
        "title": "A Nice Simple One",
        "subtitle": "Subtitle",
        "text": "text text text"
    },
    {
        "id": "2",
        "title": "A Nice Simple Two",
        "subtitle": "London",
        "text": "text text text"
    }
]

我正在尝试设置 HomeArticle 并同时创建关系

RKObjectManager* objectManager = [RKObjectManager sharedManager];
    NSString *pathString = @"/get_news/";
    [objectManager loadObjectsAtResourcePath:pathString usingBlock:^(RKObjectLoader *loader) {
        loader.delegate = self;
        loader.objectMapping = [self myHomeArticleMapping];

        loader.onDidLoadObject = ^(id object) {
            ...
        };
    }];

- (RKManagedObjectMapping *) myHomeArticleMapping{
    if(_homeArticleMapping == nil){
        _homeArticleMapping = [RKManagedObjectMapping mappingForClass:[HomeArticle class] inManagedObjectStore:[[RKObjectManager sharedManager] objectStore]];
        [_homeArticleMapping mapKeyPath:@"id" toAttribute:@"identifier"];
        [_homeArticleMapping mapRelationship:@"article" withMapping:[self myArticleMapping]];
        [_homeArticleMapping connectRelationship:@"article" withObjectForPrimaryKeyAttribute:@"identifier"];
    }
}

- (RKManagedObjectMapping *) myArticleMapping{
    if(_articleMapping == nil){
        _articleMapping = [RKManagedObjectMapping mappingForClass:[Article class] inManagedObjectStore:[[RKObjectManager sharedManager] objectStore]];

        [_articleMapping mapKeyPath:@"id" toAttribute:@"identifier"];
        [_articleMapping mapKeyPath:@"title" toAttribute:@"title"];
        [_articleMapping mapKeyPath:@"subtitle" toAttribute:@"subtitle"];
        [_articleMapping mapKeyPath:@"text" toAttribute:@"text"];
    }
}

填充“HomeArticle”的标识符,但不创建关系。 你知道我怎么能做到这一点吗? 我正在研究的项目有点复杂,但我对其进行了简化以使其更加清晰。

解决方法

好的,我设法完成这项工作的唯一方法是在方法中重新处理有效负载:

- (void)objectLoader:(RKObjectLoader*)loader willMapData:(inout id *)mappableData

我基本上从有效负载数组中创建了一个字典,并将id作为密钥:

- (void)objectLoader:(RKObjectLoader*)loader willMapData:(inout id *)mappableData {
    NSMutableArray *newsArray = [NSMutableArray arrayWithArray:(NSArray *)*mappableData];
    NSMutableDictionary *newData = [[NSMutableDictionary alloc] init];
    int elementNumber = [newsArray count];
    for(int i = 0 ; i < elementNumber ; i++){
        NSMutableDictionary *news = [NSMutableDictionary dictionaryWithDictionary:[newsArray objectAtIndex:i]];

        [newData setObject:news forKey:[news objectForKey:@"id"]];
    }
    *mappableData = newData;
}

然后我更改了“HomeArticle”的映射,如下所示:

if(_homeArticleMapping == nil){
            _homeArticleMapping = [RKManagedObjectMapping mappingForClass:[HomeArticle class] inManagedObjectStore:[[RKObjectManager sharedManager] objectStore]];
        _homeArticleMapping.forceCollectionMapping = YES;
        [_homeArticleMapping mapKeyOfNestedDictionaryToAttribute:@"identifier"];
        _homeArticleMapping.primaryKeyAttribute = @"identifier";
        [_homeArticleMapping mapKeyPath:@"(identifier)"
                         toRelationship:@"article"
                            withMapping:[self myArticleMapping]];
 }

它有效,但我不喜欢它。

1 个答案:

答案 0 :(得分:1)

说我有一个有对手关系的实体。我想用另一个名为facebookMapping的映射来映射关系。然后将映射添加到相应keyPath(或根名称)的mappingProvider。

RKManagedObjectMapping *gameEntryMapping = [RKManagedObjectMapping mappingForClass:[GameEntry class] inManagedObjectStore:[RKObjectManager sharedManager].objectStore];
RKManagedObjectMapping *facebookMapping = [RKManagedObjectMapping mappingForClass:[FacebookUser class] inManagedObjectStore:[RKObjectManager sharedManager].objectStore];
[gameEntryMapping mapRelationship:@"opponent" withMapping:facebookMapping];
[facebookMapping mapKeyPath:@"systemID" toAttribute:@"systemId"];
[facebookMapping mapKeyPath:@"name" toAttribute:@"name"];
[[RKObjectManager sharedManager].mappingProvider setMapping:gameEntryMapping forKeyPath:@"games"];
[[RKObjectManager sharedManager].mappingProvider addObjectMapping:gameEntryMapping];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/syncGames" usingBlock:^(RKObjectLoader *loader) {
    loader.method = RKRequestMethodPOST;
    loader.params = params;
    loader.delegate = self;
}];