Iphone 正在与RestKit映射对象和引用斗争
这里是我的实体Iphone 正在与RestKit映射对象和引用斗争,iphone,objective-c,core-data,restkit,relationship,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
- 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"
}
]
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]];
}
我正在尝试将家庭文章进行popolate,同时创建关系,例如:
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;
}
这是可行的,但我并不真的喜欢它。说我有一个与对手有关系的实体。我想用另一个名为facebookMapping的映射映射关系。然后在相应的键路径(或根名称)处将映射添加到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;
}];