ios - RESTKit 2.0 : Pattern string must not be empty in order to perform pattern matching

标签 ios restkit restkit-0.20

我使用下面的代码登录服务器:

//第一 block 代码

-(RKObjectManager *)getObjectManager
{
    NSURL *baseURL = [NSURL URLWithString:@"http://api.domain.com"];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:baseURL];
    RKObjectManager *manager = [[RKObjectManager alloc]initWithHTTPClient:httpClient];
    [manager.HTTPClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [manager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
    [manager.HTTPClient setParameterEncoding:AFJSONParameterEncoding];
    [RKMIMETypeSerialization registeredMIMETypes];
    return [RKObjectManager sharedManager];
}

- (void)loginUserwithUsername:(NSString *)username andPassword:(NSString *)password requestByNewUser:(BOOL)newRegistration
{
    [self getObjectManager];

    RKObjectManager *objectManager = [RKObjectManager sharedManager];
    [objectManager.HTTPClient setAuthorizationHeaderWithUsername:username password:password];

    NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
    RKMapping *mapping = [RESTMappingProvider profileMapping];
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:nil
                                                                                           keyPath:nil statusCodes:statusCodeSet];

    NSMutableURLRequest *request = [objectManager.HTTPClient requestWithMethod:@"POST"
                                                                          path:@"/login"
                                                                    parameters:@{@"username": username,
                                                                                 @"password": password
                                                                                 }];
    RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request
                                                                        responseDescriptors:@[responseDescriptor]];
    [objectManager.HTTPClient  registerHTTPOperationClass:[AFHTTPRequestOperation class]];

    [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"mappingResults Error %@", error);
        }
    }];
    [operation start];


}

登录后,我尝试发出 Google Places API 请求并收到错误:

//第二个代码块

- (void)fetchPlaces:(NSString *)input;
{
    NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
    RKMapping *mapping = [RESTMappingProvider googleAutoCompleteMapping];

    NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/json?input=%@&sensor=true&key=%@&location=0.000000,0.000000", input, self.key];
    NSString *urlStringEncoded = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlStringEncoded];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:nil
                                                                                           keyPath:@"predictions" statusCodes:statusCodeSet];

    RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request
                                                                        responseDescriptors:@[responseDescriptor]];

    [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

        self.responseObjects = mappingResult.array;
    [operation start];
}

错误:

2014-04-02 14:11:17.865 App[1247:60b] *** Assertion failure in +[RKPathMatcher pathMatcherWithPattern:], /Users/App
 Time/Pods/RestKit/Code/Support/RKPathMatcher.m:74
2014-04-02 14:11:17.868 App[1247:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Pattern string must not be empty in order to perform pattern matching.'

但是,如果我从不登录(意味着我跳过了这个问题中的第一段代码)并直接调用 Google API,则没有崩溃 API 效果很好。

我认为我正在对 RESTKit 做一些事情(可能通过创建一个 ObjectManager),通过登录导致 Google 的 API 调用导致崩溃。

我尝试运行 Charles Web 调试代理,但崩溃似乎在进行 API 调用之前就发生了。

*编辑*

我找出了导致崩溃的原因:

[[RKObjectManager sharedManager] cancelAllObjectRequestOperationsWithMethod:RKRequestMethodAny matchingPathPattern:nil];

这是试图取消所有先前的请求。

我将其替换为:

[[RKObjectManager sharedManager] cancelAllObjectRequestOperationsWithMethod:RKRequestMethodAny matchingPathPattern:@"maps/api/place/autocomplete"];

它似乎有效。

问题:此代码是否会取消之前对 https://maps.googleapis.com/maps/api/place/autocomplete/json 的任何请求?

最佳答案

当您创建 responseDescriptor 时,它会添加到您使用 pathPattern:nilRKObjectManager 中。这是不允许的。您必须指定路径模式,因为 RestKit 必须查找适当的响应描述符以应用于接收到的响应。

稍后,您再次使用 pathPattern:nil,但这是直接使用 RKObjectRequestOperation。在这种情况下,它是允许的(因此有效),因为您提供了一个明确的列表并且不需要查找。

关于ios - RESTKit 2.0 : Pattern string must not be empty in order to perform pattern matching,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22823448/

相关文章:

ios - Swift - 向下滑动时如何分页

iphone - Reskit v0.2 json 格式映射

objective-c - 第三方库如何在 Objective-C 和 Xcode 中工作?

ios - RestKit 0.24 ||获取对象路径 ||结果对象的 NSString 参数设置为 NSNull

ios - 具有不成功状态代码的 RestKit 映射

ios - Restkit - 删除具有相同 URL 路径的孤立对象

ios - RestKit:如何获取一个简单的 JSON 字符串数组?

ios - 使用 Swift 2 在 Xcode 7 中展开不起作用

ios - 警报 View 事件指示器显示时间

ios - 如何将 Google Analytics 添加到适用于 iOS 设备的 Cocoa Touch Framework