使用 RKObjectMapping 的嵌套映射
Posted
技术标签:
【中文标题】使用 RKObjectMapping 的嵌套映射【英文标题】:Nested mapping using RKObjectMapping 【发布时间】:2012-11-07 07:38:45 【问题描述】:我正在尝试使用 RKObjectMapping 执行对象映射。
这是我的 JSON 示例:
"notification":
"created_at": "2012-10-23T02:43:43-04:00",
"delivered": true,
"id": 3915,
"missed": false,
"rejected": false,
"search_request_id": 360,
"bid":
"bid_type": "none",
"bid_type_values": null,
"comment": "hi\n",
"created_at": "2012-10-23T02:43:54-04:00",
"delivered": true,
"delivery_type": "email",
"id": 268,
"has_chat_session": true,
,
"search_request":
"created_at": "2012-10-23T02:43:42-04:00",
"customer_id": 6,
"id": 360,
"latitude": "40.7536854",
"location": "10001",
"longitude": "-73.9991637",
我已经使用 jsonlint 验证了 JSON,它是有效的。
这是我的映射。
RKObjectMapping *bidMapping = [RKObjectMapping mappingForClass:[Bid class]];
[bidMapping mapKeyPath:@"comment" toAttribute:@"comment"];
[bidMapping mapKeyPath:@"chat_session_id" toAttribute:@"chatSessionId"];
[objectManager.mappingProvider setMapping:bidMapping forKeyPath:@"bid"];
RKObjectMapping *searchRequestMapping = [RKObjectMapping mappingForClass:[SearchRequest class]];
[searchRequestMapping mapKeyPath:@"purpose" toAttribute:@"purpose"];
[searchRequestMapping mapKeyPath:@"customer_Id" toAttribute:@"customerId"];
[searchRequestMapping mapKeyPath:@"need" toAttribute:@"need"];
[objectManager.mappingProvider setMapping:searchRequestMapping forKeyPath:@"search_request"];
RKObjectMapping *notificationMapping = [RKObjectMapping mappingForClass:[Notification class]]; // have not mapped all the attributes.
[notificationMapping mapKeyPath:@"id" toAttribute: @"notificationId"];
[notificationMapping mapKeyPath:@"created_at" toAttribute:@"timestamp"];
[notificationMapping mapKeyPath:@"vendor_id" toAttribute:@"vendorId"];
[notificationMapping mapKeyPath:@"bid" toRelationship:@"bid" withMapping:bidMapping];
[notificationMapping mapKeyPath:@"search_request" toRelationship:@"search_request" withMapping:notificationMapping];
[objectManager.mappingProvider setMapping:notificationMapping forKeyPath:@"notification"];
当我尝试加载ObjectsAtResourcePath 时,程序因我无法调试的断言失败而崩溃。我也尝试使用 rootKeyPaths,但无济于事。
有人可以帮我找出错误吗?
谢谢!
ps:我想添加一个 NSDictionary 参数,文档说使用 appendQueryParams:params。但我收到警告说它已被弃用。我可以使用任何替代方案吗?
【问题讨论】:
【参考方案1】:你在这里给出了错误的映射-
[notificationMapping mapKeyPath:@"search_request" toRelationship:@"search_request" withMapping:notificationMapping];
你应该给 searchRequestMapping like-
[notificationMapping mapKeyPath:@"search_request" toRelationship:@"search_request" withMapping:searchRequestMapping];
除了 appendQueryParams:params,你可以使用 RKClient 的方法下面的方法-
- (RKRequest *)get:(NSString *)resourcePath queryParameters:(NSDictionary *)queryParameters delegate:(NSObject<RKRequestDelegate> *)delegate;
【讨论】:
捂脸。对我来说真是愚蠢的错误。感谢您发现它拉胡尔。干杯! 答案已过时 嗨 Alberto,您能在此处更新/发布您的答案吗?几年前我已经停止在 ios 上工作,所以我不知道最近的变化。以上是关于使用 RKObjectMapping 的嵌套映射的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 RKObjectMapping 在嵌套的 RLMObject 中设置相同的主键?
RKObjectMapping 中的混淆设置映射 - Rest
如何使用带有 RKObjectMapping 的 RestKit 0.24 将本地 JSON 字符串映射到对象?