我对 iOS 开发很陌生,我正面临一个多线程问题。
我正在使用带有 SDWebImage 的KTPhotobrowser创建照片和视频库。我必须在每张图片上加载一些外部数据,我不想影响画廊滚动视图的流畅度。
所以,我正在尝试使用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;
}
我认为这不是正确的方法......有没有人有任何建议?