简体   繁体   中英

Cancel REST request task in UICollectionViewCell if cell is reused before successfull download

I'm using UICollectionViewCell to load and display thumbnails from the server using custom Rest Manager. This loading is not direct, it checks refresh token and creates a download task. I can't return this task from the download method.

Currently the problem is when user is scrolling fast, it will start downloading task even if this thumbnail quickly disappears from the view and it should cancel this task because it is not needed anymore.

I intended to use prepareForReuse method, but I can't figure out how to "send" some kind of cancel request to the download task for this thumbnail after it goes outside of the view.

Is there any best practice how I can solve this? Let me know if any code is needed.

EDIT #1

This is code for didEndDisplayingCell . How can I find correct task and "notify" it to cancel?

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {

    NSString *reuseIdentifier = @"collectionCellIdentifier";

    cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    [cell prepareForReuse];
}

Perhaps what is you are looking for is collectionView(_:didEndDisplaying:forItemAt:) method .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM