【发布时间】:2014-01-15 14:13:15
【问题描述】:
按照 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
啊哈 - 原来 /was/ 击中了我的服务器,日志是错误的。小学生的错误 - 对此感到抱歉。
我的响应描述符仍然设置不正确 - 我可以得到一些帮助吗?
[RKResponseDescriptor responseDescriptorWithMapping:[JGMappingProvider companyMapping]
method:RKRequestMethodPUT
pathPattern:@"companies/:companyId/approve"
keyPath:@"response"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
我似乎无法让 pathPattern 匹配 - 理想情况下,我会有一个匹配 Companies/:companyId/approve & Companies/:companyId/reject 的通配符,但我看不到如何在文档。
为愚蠢的问题道歉!
编辑:完成!我正在使用
responseDescriptorWithMapping: method: pathPattern: keyPath: statusCodes:
- 一切都与(已弃用?)
responseDescriptorWithMapping: pathPattern: keyPath: statusCodes:
【问题讨论】:
标签: ios core-data restkit restkit-0.20