2017-03-29 13 views
0

PageViewController私はコンテンツとして複数のイメージを持っています。私はサービスから次のようなイメージを得ています。ただし、ネットワーク操作がまだ進行中にユーザーがView Controllerを閉じるためにクリックすると、アプリケーションがクラッシュします。NSOperationQueueはキャンセルされていません

queue = [[NSOperationQueue alloc] init]; 
operation = [NSBlockOperation blockOperationWithBlock:^{ 
      [self addAllImages]; 
       dispatch_sync(dispatch_get_main_queue(), ^(void) { 
        [self pageViewcontrollerSetup]; 
      }); 
     }]; 
    [queue addOperation:operation]; 
} 

- (void) addAllImages 
{ 
    self.pageImages =[[NSMutableArray alloc] initWithCapacity:self.pageControl.numberOfPages]; 

    for(id key in [[[pElements objectAtIndex:self.selectedIndexPath.row]objectForKey:@"pdetail"] objectForKey:@"images"]) { 
     NSString *productURL = [NSString stringWithFormat:@"%@%@", PRODUCT_URL, [[[[pElements objectAtIndex:self.selectedIndexPath.row]objectForKey:@"pdetail"] objectForKey:@"images"] objectForKey:key]]; 
     NSData* productData = [NSData dataWithContentsOfURL:[NSURL URLWithString:productURL]]; 

     if ([UIImage imageWithData:productData]) { 
       [self.pageImages addObject:[UIImage imageWithData:productData]]; 
     } 
    } 
} 

- (void)closeBtnClicked { 

    [queue cancelAllOperations]; 
} 

注:私はGCD(グランドセントラル派遣)マルチスレッドのために使っていたが、私はあなたがそれを取り消すことができないことを知っているようになったは、それゆえ私は、操作がないキャンセルNSOperationQueue

答えて

0

にswicthedあなたのコードは魔法のようにトラックで止められます。操作がキャンセルされているかどうかを定期的に確認し、その場合には暴走することは、コードまでです。

+0

私に小さな例を教えていただけますか?私は 'cancelAllOperations'が対応するスレッドを直ちにキャンセルすると思った。そうでなければ、 '[queue cancelAllOperations];'を使う目的は何ですか? – hotspring

+0

私が投稿したリンクを見ましたか?操作ブロック内のキャンセルを確認する方法を示します。 – matt

関連する問題