AFJSONRequestOperation崩溃数据参数为nil

时间:2013-03-19 04:01:11

标签: ios objective-c afnetworking

我遇到一个奇怪的问题,只有在我运行我的应用程序时间歇性地发生。我试图使用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;
}

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

[request setHTTPMethod:@"get"]; // post, patch ....