0

我遇到了一个奇怪的问题,只有在我运行我的应用程序时才会间歇性发生。我正在尝试使用 AFNetworking 从两个不同的来源提取 JSON。有时,当操作运行时,应用程序会在*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'登陆时崩溃json_request_operation_processing_queue

我希望这不是 AFNetworking 的问题,我只是做错了一些事情。以下是我认为相关的方法(JSONManager 扩展了 AFHTTPClient):

+ (JSONManager *) sharedJSONManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedJSONManagerInsance = [[JSONManager alloc] initWithBaseURL:[NSURL URLWithString:sourceUrl1]];
    });
    return _sharedJSONManagerInsance;
}

- (void) loadOperations {
    _sharedJSONManagerInsance.operations = [NSMutableArray arrayWithCapacity:2];
    [_sharedJSONManagerInsance.operations addObject:[self fetchJSON:sourceUrl1]];
    [_sharedJSONManagerInsance.operations addObject:[self fetchJSON:sourceUrl2]];
}

- (void) executeOperations {
    [_sharedJSONManagerInsance loadOperations];
    _sharedJSONManagerInsance.fetchedStories = [[NSMutableArray alloc] init];
    [self enqueueBatchOfHTTPRequestOperations:operations
                                progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
                                    NSLog(@"Finished %d of %d", numberOfFinishedOperations, totalNumberOfOperations);
                                }
                              completionBlock:^(NSArray *operations) {
                                  [[CoreDataManager sharedManager] persistFetchedStories:_sharedJSONManagerInsance.fetchedStories];
                                  _sharedJSONManagerInsance.operations = nil;
                                  NSLog(@"All operations finished");
                              }];
}

- (AFHTTPRequestOperation *)fetchJSON:(NSString*)requestUrl {

    NSURL* jsonUrl = [NSURL URLWithString:requestUrl];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:jsonUrl];
    AFJSONRequestOperation *operation = nil;

    operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        if([requestUrl isEqualToString:sourceUrl1]) {

            NSArray* arr = [[JSON valueForKeyPath:@"data"] valueForKey:@"children"];
            for (NSDictionary *item in arr) {
                FetchedStory* fs = [[FetchedStory alloc] init];
                fs.title = [[item valueForKey:@"data"]valueForKey:@"title"];
                fs.url = [[item valueForKey:@"data"]valueForKey:@"url"];
                fs.score = [[item valueForKey:@"data"]valueForKey:@"score"];
                fs.source = @"source1";
                [self.fetchedStories addObject:fs];
            }
        }
        else if([requestUrl isEqualToString:sourceUrl2]) {
            NSArray* arr = [JSON valueForKeyPath:@"items"];
            for (NSDictionary *item in arr) {
                FetchedStory* fs = [[FetchedStory alloc] init];
                fs.title = [item valueForKey:@"title"];
                fs.url = [item valueForKey:@"url"];
                NSString* scoreString = [item valueForKey:@"score"];
                if(scoreString != nil && [scoreString length]!=0) {
                    NSRange spaceRange = [scoreString rangeOfString:@" "];
                    scoreString = [scoreString substringToIndex:spaceRange.location];
                    fs.score = [NSDecimalNumber decimalNumberWithString:scoreString];
                    fs.source = @"source2";
                    [self.fetchedStories addObject:fs];
                }
            }
        }
    } failure:nil];

    return operation;
}

崩溃发生在“所有操作完成”记录到控制台之前。同样,这只会在某些时候发生。

4

2 回答 2

0

看起来这是 AFJSONRequestOperation 的 responseJSON 方法的错误。我添加了一个零检查,这似乎是一个很好的创可贴。

于 2013-03-20T00:47:54.690 回答
0

实际上你忘记设置 HTTP 方法参数它应该是这样的:

[request setHTTPMethod:@"get"]; // post, patch ....
于 2013-06-17T16:20:15.093 回答