我遇到一個奇怪的問題,只有當我跑我的應用程序發生間歇性。我試圖從兩個不同的來源,使用AFNetworking拉下JSON。偶爾,當操作運行時,應用程序將與*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
登陸上崩潰json_request_operation_processing_queue
。AFJSONRequestOperation崩潰數據參數是零
我希望這不符合AFNetworking一個問題,我只是做一些不正確。這裏是我的方法,我認爲是relavent(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;
}
碰撞發生之前「的所有操作完成的」日誌到控制檯。再次,這隻發生在一些時間。
你可以嘗試把一個斷點在成功處理程序和步驟,通過它來查找該行正在生成異常。 – pckill 2013-03-19 11:14:00