我收到以下代码的错误,我正在使用NSOperationQueue从服务器获取发布数据。在登录时我想从服务器获得响应总共8次。为此,我使用的是NSOperationQueue,但它无法正常工作。有时我收到此错误
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
self.operationQueue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(getDataFromServer2)
object:nil];
[self.operationQueue addOperation:operation];
// The same story as above, just tell here to execute the colorRotatorTask method.
operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(getDataFromServer3)
object:nil];
[self.operationQueue addOperation:operation];
operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(getDataFromServer4)
object:nil];
[self.operationQueue addOperation:operation];
operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(getDataFromServer5)
object:nil];
[self.operationQueue addOperation:operation];
operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(getDataFromServer6)
object:nil];
[self.operationQueue addOperation:operation];
operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(getDataFromServer7)
object:nil];
[self.operationQueue addOperation:operation];
operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(getDataFromServer8)
object:nil];
[self.operationQueue addOperation:operation];
我的JSON代码是
- (void) getDataFromServer2 {
NSString *str = [NSString stringWithFormat:@"http://***********?”];
NSLog(@"%@", str);
self.urlSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
self.url = [NSURL URLWithString:str];
self.urlReq = [[NSMutableURLRequest alloc]initWithURL:self.url];
[self.urlReq setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[self.urlReq setHTTPMethod:@"POST"];
NSString *post = [[NSString alloc]initWithFormat:@“*=&&”];
NSLog(@“Post : %@", post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long) [postData length]];
[self.urlReq setValue:postLength forHTTPHeaderField:@"Content-Length"];
[self.urlReq setHTTPBody:postData];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
self.dataTask = [self.urlSession dataTaskWithRequest:self.urlReq completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!(data == nil)) {
NSError *error;
self.loginDic2 = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"%@", error);
self.integer2 = [[self.loginDic2 objectForKey:@"id"] count];
NSLog(@"self.integer2 : %lu", self.integer2);
NSLog(@"Login Dic2 : %@", self.loginDic2);
if ([[self.loginDic2 objectForKey:@"status"] isEqualToString:@"SUCCESS"]) {
self.ID = [[NSMutableArray alloc]init];
self.m = [[NSMutableArray alloc]init];
dispatch_async(dispatch_get_main_queue(), ^{
for (int i=0; i<self.integer2; i++) {
[self.ID addObject:[[[self.loginDic2 objectForKey:@“d”] objectAtIndex:i] objectForKey:@"id"]];
[self.m addObject:[[[self.loginDic2 objectForKey:@“d”] objectAtIndex:i] objectForKey:@"m"]];
}
NSLog(@"cast_nm : %@", self.m);
[self.defaults setObject:self.cast_nm forKey:@"m"];
[self.defaults synchronize];
});
} else {
NSString *str = [self.loginDic2 objectForKey:@"error_code"];
if ([str isEqualToString:@"104"]) {
self.noRecordsFound = @"No Records Found.";
} else {
self.noRecordsFound = @"Invalid Parameters.";
}
UIAlertController * alert = [UIAlertController alertControllerWithTitle : @"Message"
message : self.noRecordsFound
preferredStyle : UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{ }];
[alert addAction:ok];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:alert animated:YES completion:nil];
});
}
} else {
UIAlertController * alert = [UIAlertController alertControllerWithTitle : @"Message"
message : @"No server response."
preferredStyle : UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{ }];
[alert addAction:ok];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:alert animated:YES completion:nil];
});
}
}]; [self.dataTask resume];
});
}
我使用相同的代码8次....
答案 0 :(得分:0)
问题是self.ID addObject
或self.m addObject
。看来有时您尝试插入的对象是nil
,而您无法将nil
插入数组中。
我可以在您的代码中看到的其他问题是您在块内部使用了对self
的强引用,这可能会导致内存泄漏。