2013-03-19 53 views
0

我遇到一個奇怪的問題,只有當我跑我的應用程序發生間歇性。我試圖從兩個不同的來源,使用AFNetworking拉下JSON。偶爾,當操作運行時,應用程序將與*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'登陸上崩潰json_request_operation_processing_queueAFJSONRequestOperation崩潰數據參數是零

我希望這不符合AFNetworking一個問題,我只是做一些不正確。這裏是我的方法,我認爲是re​​lavent(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; 
} 

碰撞發生之前「的所有操作完成的」日誌到控制檯。再次,這隻發生在一些時間。

+1

你可以嘗試把一個斷點在成功處理程序和步驟,通過它來查找該行正在生成異常。 – pckill 2013-03-19 11:14:00

回答

0

看起來這與AFJSONRequestOperation的responseJSON方法的錯誤。我添加了一張零支票,這似乎是一個很好的bandaid。

+0

你在哪裏添加此零檢查? – Bot 2015-03-11 20:54:29

+0

這是兩年前所以很遺憾,我不記得了。我可以說我正在使用AFNetworking 1.x,這與2.x無關。 – hodgesmr 2015-03-11 21:08:09

0

事實上,你忘記設置HTTP方法參數就應該是這樣的:

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