我正在使用一个库,在后台执行一些工作,然后调用完成处理程序。所有非常标准。取消完成处理程序块运行
[[LastFm sharedInstance] getInfoForArtist:@"Pink Floyd" successHandler:^(NSDictionary *result) {
// Do stuff...
} failureHandler:nil];
我实际上在tableview中使用这个:在每个单元格(子类)中,我得到关于艺术家的信息并显示它。这也是问题:当单元格被移出屏幕并重新用于其他艺术家时,前一个艺术家的successHandler仍然可以执行,从而导致标签和图像快速连续多次更改。
我的想法是创建一个NSOperationQueue,加上其内部的getInfoForArtist电话,并确保它可以被取消:
NSBlockOperation *operation = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakOperation = operation;
[operation addExecutionBlock:^{
[[LastFm sharedInstance] getInfoForArtist:mediaItem.artist successHandler:^(NSDictionary *result) {
if (weakOperation.isCancelled) {
return;
}
// Do stuff...
} failureHandler:nil];
}];
[self.queue addOperation:operation];
的问题是,weakOperation
总是successHandler内空。如果我将其更改为__block
而不是__weak
,则weakOperation
是正确的实例,但其isCancelled
状态始终为NO
。
我在正确的时间打电话给[self.queue cancelAllOperations];
,当时单元格被移出屏幕。
所以我的问题是,如何防止在单元格被其他艺术家重用之后运行successHandler?