在RKGist示例应用程序之后,我设法通过RestKit让我的API与Core Data同步。
这仅适用于一个具有关联的模型;我正在尝试添加额外的路线,但却无法使其正常工作。
AppDelegate.m
- (void)setupRestKit
{
// ...
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[JGMappingProvider personMapping]
method:RKRequestMethodGET
pathPattern:@"people"
keyPath:@"response"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[JGMappingProvider companyMapping]
method:RKRequestMethodGET
pathPattern:@"companies"
keyPath:@"response"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
// add some routes
[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithName:@"approve_company" pathPattern:@"companies/:companyId/approve" method:RKRequestMethodPUT]];
...
}
JGMappingProvider.m
+ (RKMapping *)companyMapping
{
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Company"
inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];
[mapping addAttributeMappingsFromDictionary:@{
@"id": @"companyId",
@"name": @"name",
@"looking_for": @"lookingFor",
@"location": @"location",
@"updated_at": @"updatedAt",
@"created_at": @"createdAt"}];
mapping.identificationAttributes = @[ @"companyId" ];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"person" toKeyPath:@"person" withMapping:[self personMapping]]];
return mapping;
}
+ (RKMapping *)personMapping
{
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Person"
inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];
[mapping addAttributeMappingsFromDictionary:@{
@"name": @"name",
@"nickname": @"nickname",
@"id": @"personId",
@"email": @"email"}];
mapping.identificationAttributes = @[ @"personId" ];
return mapping;
}
@end
一切正常。但是,当我尝试添加一些额外的路线时......
ViewController.m
- (void)approve
{
NSMutableURLRequest *request = [[RKObjectManager sharedManager] requestWithPathForRouteNamed:@"approve_company" object:_company parameters:nil];
[self sendRequestWithSharedManager:request];
}
- (void)reject
{
NSMutableURLRequest *request = [[RKObjectManager sharedManager] requestWithPathForRouteNamed:@"reject_company" object:_company parameters:nil];
[self sendRequestWithSharedManager:request];
}
- (void)sendRequestWithSharedManager:(NSMutableURLRequest *)request {
RKObjectRequestOperation* operation = [[RKObjectManager sharedManager] objectRequestOperationWithRequest:(NSURLRequest *)request success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"success!");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"%@", [error description]);
}];
operation.targetObject = nil; // this was suggested in a tutorial, has no affect
NSLog(@"Requested %@", [operation.HTTPRequestOperation.request.URL description]); // this is the correct URL, all the time
[[RKObjectManager sharedManager] enqueueObjectRequestOperation:operation];
}
大部分时间都失败了。具体来说,前两个请求似乎失败了 - 根本没有点击我的服务器(我在服务器端有一些断点要调试),但是在第三个请求它完全正常工作。
我很困惑 - 如果它第三次运作,那么可能会设置正确的东西?
以下是日志 - 它们有点混乱&我真的不知道我在找什么。
https://gist.github.com/jongd/d28b5f58a901270999f2/raw/839f1b529538ab1814a49350c33b0057cb5b98fc/logger.txt
Ahah - 结果是/正在/击中我的服务器,日志错误。小学生错误 - 抱歉。
我仍然遇到无法正确设置响应描述符的问题 - 我可以获得一些帮助吗?
[RKResponseDescriptor responseDescriptorWithMapping:[JGMappingProvider companyMapping]
method:RKRequestMethodPUT
pathPattern:@"companies/:companyId/approve"
keyPath:@"response"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
我似乎无法让pathPattern匹配 - 理想情况下我会有一个匹配公司的通配符/:companyId / approve&公司/:companyId / reject但我在文档中看不到如何做到这一点。
为愚蠢的问题道歉!
编辑:完成!我在用responseDescriptorWithMapping: method: pathPattern: keyPath: statusCodes:
- 让它全部使用(已弃用?)
responseDescriptorWithMapping: pathPattern: keyPath: statusCodes:
答案 0 :(得分:1)
首先,你确定你没有点击服务器 - 根据你的日志它似乎到达服务器:
'http://localhost:1313/api/1/companies/19/approve' (200 OK / 0 objects) [request=1.0492s mapping=0.0000s total=1.0514s]
您的第二个RKResponseDescriptor(公司名称)未正确设置似乎存在问题。
您是否可以尝试不重用本地变量responseDescriptor来创建和传递第二个RKResponseDescriptor。
答案 1 :(得分:1)
我假设您的基本网址为http://localhost:1313/api/1/
。
您的所有3个请求都有一个网址:'http://localhost:1313/api/1/companies/19/approve'
RestKit记录"No response descriptors match the response loaded."
。这意味着在所有响应描述符中,没有一个具有匹配companies/19/approve
的路径模式。你需要一个。
第3个请求没有成功使用任何映射...所有3个请求确实命中了服务器(并且您可以在日志中看到响应内容。
请记住,路由是用于创建请求路径的。通常通过属性注入(因此注入companies/19/approve
的路径19
)。
响应描述符仅通过它们使用请求路径模式链接到路由。无论何时发出请求,您都需要一个与该请求匹配的响应描述符。 (nil
的响应描述符路径模式匹配所有内容。)