Ios ViewController委托和ViewController定时问题
我正在使用Apple的lazy loader代码示例将图像加载到TableView中。由于视图控制器位于导航堆栈中,用户可以快速滚动,然后点击后退并导航出视图。VC是IconDownloader类的一个委托,该类负责图像下载,我正在VC dealloc中将IconDownloader委托设置为nil 但是,存在一个计时问题,滚动代理ScrollViewDiEndDecelerating会触发图像加载,但在“我的视图将消失”触发之间,图像加载事件已经排队。这会在视图消失时导致崩溃,委托也一样,但IconDownloader仍会激发其委托方法 因此,命令如下:Ios ViewController委托和ViewController定时问题,ios,uiviewcontroller,delegates,null,image-loading,Ios,Uiviewcontroller,Delegates,Null,Image Loading,我正在使用Apple的lazy loader代码示例将图像加载到TableView中。由于视图控制器位于导航堆栈中,用户可以快速滚动,然后点击后退并导航出视图。VC是IconDownloader类的一个委托,该类负责图像下载,我正在VC dealloc中将IconDownloader委托设置为nil 但是,存在一个计时问题,滚动代理ScrollViewDiEndDecelerating会触发图像加载,但在“我的视图将消失”触发之间,图像加载事件已经排队。这会在视图消失时导致崩溃,委托也一样,但I
// call our delegate and tell it that our icon is ready for display
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(appImageDidLoad:)])
{
[delegate appImageDidLoad:self.indexPathInTableView];
}
离开视图控制器时是否取消所有已处理的下载 我在dealloc方法中所做的是:
for all iconDownloaders (I have a list of them){
iconDownloader.delegate = nil;
[iconDownloader cancelDownload];
}
其中cancelDownload是iconDownloader类中的方法,类似于:
- (void)cancelDownload
{
[self.connection cancel]; //NSURLConnection
self.connection = nil;
self.receivedData = nil; //data collected while downloading in NSURLConnection Delegate Methods
}
我对这个示例进行了一些小的修改,它对我来说非常好
我同意你评论中的这句话:
“大多数帖子建议不要保留IconDownloader中的代理。”是的,我已经取消了我视图中的所有图像下载将消失(我将其从原始帖子中删除),但在从屏幕行加载图像之前仍会触发,因此会加载一批新的图标。
- (void)cancelDownload
{
[self.connection cancel]; //NSURLConnection
self.connection = nil;
self.receivedData = nil; //data collected while downloading in NSURLConnection Delegate Methods
}