错误:“无法添加 keyPath 新闻的映射,已经存在......”

发布于 2024-12-06 12:08:18 字数 2615 浏览 1 评论 0原文

我正在使用 RestKit 从 Restful Web 服务获取数据。使用平面文件效果很好。如果我想获得对嵌套 JSON 的响应,我的问题就开始了。

我的 JSON 如下所示:

[{"homename":"Alien","created_at":"2011-09-15T12:46:37Z", "updated_at":"2011-09-15T12:46:37Z","gametype":"Final match", "id":1,"date":"2016-10-10","guestname":"Predator", "news":[{"minute":null,"created_at":"2011-09-15T13:19:51Z","player":null,"title":"Title", "updated_at":"2011-09-15T13:19:51Z","id":1,"bodytext":"News","game_id":1},{"minute":null,"created_at":"2011-09-15T13:22:06Z","player":null,"title":"New news","updated_at":"2011-09-15T13:22:06Z","id":2,"bodytext":"Old socks","game_id":1},{"minute":null,"created_at":"2011-09-15T13:26:32Z","player":null,"title":"another title","updated_at":"2011-09-15T13:26:32Z","id":3,"bodytext":"Bodytext 2","game_id":1},{"minute":null,"created_at":"2011-09-22T12:35:19Z","player":null,"title":"comment","updated_at":"2011-09-22T12:35:19Z","id":4,"bodytext":"body of the comment","game_id":1}]}]

通过以下代码,我想获取映射的数据。

RKLogConfigureByName("RestKit/Network*", RKLogLevelTrace);
// Initialize RestKit
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://X.X.X.X:3000"];

// Enable automatic network activity indicator management
//objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;


RKObjectMapping* newsMapping = [RKObjectMapping mappingForClass:[News class]];
[newsMapping mapKeyPath:@"id" toAttribute:@"id"];
[newsMapping mapKeyPath:@"minute" toAttribute:@"minute"];
[newsMapping mapKeyPath:@"title" toAttribute:@"title"];
[newsMapping mapKeyPath:@"bodytext" toAttribute:@"bodytext"];

// Setup our object mappings
RKObjectMapping* gameMapping = [RKObjectMapping mappingForClass:[Game class]];
//  [gameMapping mapKeyPath:@"id" toAttribute:@"id"];

[gameMapping mapKeyPath:@"guestname" toAttribute:@"guestname"];
[gameMapping mapKeyPath:@"homename" toAttribute:@"homename"];
[gameMapping mapKeyPath:@"date" toAttribute:@"date"];
[gameMapping mapKeyPath:@"gametype" toAttribute:@"gametype"];
[gameMapping mapKeyPath:@"news" toAttribute:@"news"];

[gameMapping mapKeyPath:@"news" toRelationship:@"news" withMapping:newsMapping];
[objectManager.mappingProvider setMapping:gameMapping forKeyPath:@"games"];

声明 [gameMapping mapKeyPath:@"news" toRelationship:@"news" withMapping:newsMapping]; 在运行时引发异常,我无法弄清楚为什么:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to add mapping for keyPath news, one already exists...'

有人看到我做错了什么吗? 1:n 关系是否错误?

I am using RestKit to get data from a Restful Web Service. With a flat file it works great. My problem starts if I want to get the response to a nested JSON.

My JSON looks like:

[{"homename":"Alien","created_at":"2011-09-15T12:46:37Z", "updated_at":"2011-09-15T12:46:37Z","gametype":"Final match", "id":1,"date":"2016-10-10","guestname":"Predator", "news":[{"minute":null,"created_at":"2011-09-15T13:19:51Z","player":null,"title":"Title", "updated_at":"2011-09-15T13:19:51Z","id":1,"bodytext":"News","game_id":1},{"minute":null,"created_at":"2011-09-15T13:22:06Z","player":null,"title":"New news","updated_at":"2011-09-15T13:22:06Z","id":2,"bodytext":"Old socks","game_id":1},{"minute":null,"created_at":"2011-09-15T13:26:32Z","player":null,"title":"another title","updated_at":"2011-09-15T13:26:32Z","id":3,"bodytext":"Bodytext 2","game_id":1},{"minute":null,"created_at":"2011-09-22T12:35:19Z","player":null,"title":"comment","updated_at":"2011-09-22T12:35:19Z","id":4,"bodytext":"body of the comment","game_id":1}]}]

With the following code I would like to get the data mapped.

RKLogConfigureByName("RestKit/Network*", RKLogLevelTrace);
// Initialize RestKit
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://X.X.X.X:3000"];

// Enable automatic network activity indicator management
//objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;


RKObjectMapping* newsMapping = [RKObjectMapping mappingForClass:[News class]];
[newsMapping mapKeyPath:@"id" toAttribute:@"id"];
[newsMapping mapKeyPath:@"minute" toAttribute:@"minute"];
[newsMapping mapKeyPath:@"title" toAttribute:@"title"];
[newsMapping mapKeyPath:@"bodytext" toAttribute:@"bodytext"];

// Setup our object mappings
RKObjectMapping* gameMapping = [RKObjectMapping mappingForClass:[Game class]];
//  [gameMapping mapKeyPath:@"id" toAttribute:@"id"];

[gameMapping mapKeyPath:@"guestname" toAttribute:@"guestname"];
[gameMapping mapKeyPath:@"homename" toAttribute:@"homename"];
[gameMapping mapKeyPath:@"date" toAttribute:@"date"];
[gameMapping mapKeyPath:@"gametype" toAttribute:@"gametype"];
[gameMapping mapKeyPath:@"news" toAttribute:@"news"];

[gameMapping mapKeyPath:@"news" toRelationship:@"news" withMapping:newsMapping];
[objectManager.mappingProvider setMapping:gameMapping forKeyPath:@"games"];

The statement
[gameMapping mapKeyPath:@"news" toRelationship:@"news" withMapping:newsMapping];
raises an exception during runtime and I cannot figure out why:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to add mapping for keyPath news, one already exists...'

Does someone see what I am doing wrong? Is the relation wrong for a 1:n relation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你又不是我 2024-12-13 12:08:18

您有两个 news keyPath: 映射

[gameMapping mapKeyPath:@"news" toAttribute:@"news"];

[gameMapping mapKeyPath:@"news" toRelationship:@"news" withMapping:newsMapping];

删除第一个,看看是否有帮助。

干杯。

You have two mappings for news keyPath:

[gameMapping mapKeyPath:@"news" toAttribute:@"news"];

and

[gameMapping mapKeyPath:@"news" toRelationship:@"news" withMapping:newsMapping];

remove the first and see if it helps.

Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文