私は iOS 開発が初めてで、マルチスレッドの問題に直面しています。
KTPhotobrowserとSDWebImageを使用して、写真とビデオのギャラリーを作成しています。各画像に外部データをロードする必要があり、ギャラリーのスクロール ビューの滑らかさに影響を与えたくありません。
NSOperation
そのため、 andを使用してそれを実行しようとしていますが、NSOperationQueue
正しく行っているかどうかはわかりません。
私が望むのは、ユーザーが画像にとどまらずスクロールし続けない場合、読み込みプロセスを停止することです。
私の現在のコード:
//setCurrentIndex is called when the scrollView is scrolled
- (void)setCurrentIndex:(NSInteger)newIndex {
[loadingQueue_cancelAllOperations];
currentIndex_ = newIndex;
[self loadPhoto:currentIndex_];
NSInvocationOperation *InvocationOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadInfosAtIndex:) object:newIndex];
[loadingQueue_ addOperation:InvocationOp];
[InvocationOp release];
[selfloadPhoto:currentIndex_ + 1];
[selfloadPhoto:currentIndex_ - 1];
[selfunloadPhoto:currentIndex_ + 2];
[selfunloadPhoto:currentIndex_ - 2];
}
-(void) loadInfosAtIndex:(NSInteger)index {
if (index < 0 || index >= photoCount_) {
return;
}
KTPhotoView* photoView = [photoViews_ objectAtIndex:index];
if([photoView infosAlreadyLoaded]){
NSLog(@"%d Already Loaded", index);
return;
}
//Get the extra information by calling a web service
photoView.infosAlreadyLoaded = YES;
}
これは適切な方法ではないと思います...誰かアドバイスはありますか?