在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
啊-原来是/正在/打我的服务器,日志是错误的。小学生错误-对此感到抱歉。
我仍然有我的响应描述符设置不正确的问题-我可以在这方面获得帮助吗?
[RKResponseDescriptor responseDescriptorWithMapping:[JGMappingProvider companyMapping]
method:RKRequestMethodPUT
pathPattern:@"companies/:companyId/approve"
keyPath:@"response"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
我似乎无法使pathPattern匹配-理想情况下,我将具有一个匹配company /:companyId / approve&companies /:companyId / reject的通配符,但我看不到如何在文档中进行匹配。
抱歉的愚蠢问题!
编辑:完成!我在用
responseDescriptorWithMapping: method: pathPattern: keyPath: statusCodes:
-使用(已弃用?)responseDescriptorWithMapping: pathPattern: keyPath: statusCodes:
最佳答案
首先,您确定没有击中服务器-根据您的日志,它似乎可以到达服务器:
'http://localhost:1313/api/1/companies/19/approve' (200 OK / 0 objects) [request=1.0492s mapping=0.0000s total=1.0514s]
您的第二个RKResponseDescriptor(公司专用的RKResponseDescriptor)似乎设置不正确,这似乎是一个问题。
您是否可以尝试不重用局部变量responseDescriptor来创建和传递第二个RKResponseDescriptor。