gpt4 book ai didi

iphone - 与 RestKit 映射对象和引用作斗争

转载 作者:太空狗 更新时间:2023-10-30 03:45:36 25 4
gpt4 key购买 nike

这是我的实体

- 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]];
}

它有效,但我不太喜欢它。

最佳答案

假设我有一个具有对手关系的实体。我想用另一个名为 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;
}];

关于iphone - 与 RestKit 映射对象和引用作斗争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12713992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com