Restkit 元数据没有映射

Posted

技术标签:

【中文标题】Restkit 元数据没有映射【英文标题】:Restkit metadata isn't mapping 【发布时间】:2016-01-20 15:31:20 【问题描述】:

我正在努力获得与元数据的正确映射...除了元数据 url 参数之外,所有内容都完美映射。

这是我的实体映射:

RKEntityMapping *statisticsMapping = [RKEntityMapping mappingForEntityForName:@"Statistics" inManagedObjectStore:managedObjectStore];
[statisticsMapping addAttributeMappingsFromArray:@[@"fromDate", @"toDate", @"visits", @"newVisits"]];
[statisticsMapping addAttributeMappingsFromDictionary:@
                                                        @"@metadata.routing.parameters.randomId" : @"applicationRandomId"
                                                        ];
[statisticsMapping addConnectionForRelationship:@"application" connectedBy:@"applicationRandomId"];

响应描述符:

RKResponseDescriptor *ststResp = [RKResponseDescriptor responseDescriptorWithMapping:statisticsMapping
                                                                                        method:RKRequestMethodGET
                                                                                   pathPattern:@"api/apps/:randomId/statistics"
                                                                                       keyPath:@"details"
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

路由:

[objectManager.router.routeSet addRoute:[RKRoute routeWithClass:[Statistics class] pathPattern:@"api/apps/:randomId/statistics" method:RKRequestMethodGET]];

以及我如何获得它们:

NSString *url = [NSString stringWithFormat:@"/api/apps/%@/statistics",applicationRandomId];
NSMutableURLRequest *statistics = [[RKObjectManager sharedManager] requestWithObject:@"Statistics" method:RKRequestMethodGET path:url parameters:@
                                                                                                                                                         @"fromDate" : @"01-01-2016",
                                                                                                                                                         @"toDate" : @"20-01-2016"
                                                                                                                                                         ];

RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] managedObjectRequestOperationWithRequest:statistics managedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)      

 failure:^(RKObjectRequestOperation *operation, NSError *error) 

];

我还尝试混合各种 Routes 和 getObjectsAt 等...

问候,阿德里安。

【问题讨论】:

【参考方案1】:

解决方案:

[objectManager.router.routeSet addRoute:[RKRoute routeWithName:@"statistics" pathPattern:@"api/apps/:randomId/statistics" method:RKRequestMethodGET]];

[[RKObjectManager sharedManager] getObjectsAtPathForRouteNamed:@"statistics" object:@@"randomId" : applicationRandomId parameters:nil
                                                                                                      success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) 

                                                                                                      failure:^(RKObjectRequestOperation *operation, NSError *error) 

                                                                                                     ];

呃不知道它是这样工作的。

【讨论】:

以上是关于Restkit 元数据没有映射的主要内容,如果未能解决你的问题,请参考以下文章

将训练数据映射到元数据

没有存储在数据库中的 RestKit 对象映射

开源组件:用元数据和BeanUtils写一个简单的ORM映射BaseDAO.java

RestKit 两个连续的 enqueueBatchOfObjectRequestOperations 没有直接映射到核心数据

RestKit 数据映射导致应用程序崩溃

RestKit“无法在没有数据源的情况下执行关系映射”来自本地 JSON 文件