RESTKit 2.0:模式字符串不能为空以执行模式匹配

时间:2014-04-02 21:27:22

标签: ios restkit restkit-0.20

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

//第一个区块代码

-(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 Debug Proxy,但是在进行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

1 个答案:

答案 0 :(得分:1)

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

稍后,您再次使用pathPattern:nil,但这是直接使用RKObjectRequestOperation。在这种情况下,它是允许的(因此可以工作),因为您提供了一个显式列表,不需要查找。