2017-07-12 7 views
0

次のコードでエラーが発生しました。NSOperationQueueを使用してサーバーからポストデータを取得しています。ログイン時に私はサーバーから全部で8回応答を得たいと思う。これはNSOperationQueueを使用していますが、正しく動作していません。いくつかの時間が、私はこのエラーを取得していますNSOperation Queueを目的のCで使用するにはどうすればよいですか?

*** 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]; 

MY JSONコードは

私は同じコードを8回を使用してミリアンペア
- (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]; 
}); 
} 

....

+1

NSURLSessionを使用する場合、NSOperationQueueを使用する必要はありません。なぜなら、NSURLSessionはoperationQueueであるためです。 delegateQueueをnilに設定すると、デフォルトのタイプ(つまりシリアル・キュー)になります。 – GeneCode

+0

Arrayに値を格納するときにエラーが発生しています。だから私はなぜキューを使用している – iOS

+0

それは他の原因によって引き起こされるかもしれません。私の推測は、NSOperationQueueを使用しないことによって引き起こされたものではないということです。このコードを8回実行すると、8つのコードが連続して実行され、順序は不明です。だからあなたはそれを手作業で処理する必要があります。 – GeneCode

答えて

0

問題がself.ID addObjectまたはself.m addObjectではあります。挿入しようとしているオブジェクトがnilであり、配列にnilを挿入できないことがあります。

他の問題は、メモリリークの原因となる可能性のあるブロック内でselfを強く参照していることです。

関連する問題