Skip to content

Commit

Permalink
Cancel image download task on cell reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
pietrocaselani committed Jan 13, 2019
1 parent aa513ab commit 2936cc6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 6 additions & 3 deletions CouchTrackerApp/Shows/Progress/ShowProgressCell.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Cartography
import CouchTrackerCore
import Kingfisher
import RxSwift

final class ShowProgressCell: TableViewCell {
static let identifier = "ShowProgressCell"
private var imageTask: DownloadTask?
private var disposable: Disposable?

var presenter: ShowProgressCellPresenter? {
Expand All @@ -17,10 +19,11 @@ final class ShowProgressCell: TableViewCell {
}

override func prepareForReuse() {
super.prepareForReuse()

imageTask?.cancel()
posterImageView.image = nil
disposable = nil

super.prepareForReuse()
}

private func handleViewState(_ viewState: ShowProgressCellViewState) {
Expand All @@ -41,7 +44,7 @@ final class ShowProgressCell: TableViewCell {
}

private func showPosterImage(with url: URL) {
posterImageView.kf.setImage(with: url)
imageTask = posterImageView.kf.setImage(with: url)
}

let posterImageView = UIImageView()
Expand Down
8 changes: 5 additions & 3 deletions CouchTrackerApp/Views/PosterAndTitleCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import Kingfisher

public final class PosterAndTitleCell: CollectionViewCell {
public static let identifier = "PosterAndTitleCell"
private var imageTask: DownloadTask?

public var presenter: PosterCellPresenter! {
didSet {
imageTask?.cancel()
posterImageView.image = nil
presenter.viewWillAppear()
}
}

public override func prepareForReuse() {
super.prepareForReuse()

imageTask?.cancel()
posterImageView.image = nil
super.prepareForReuse()
}

public let posterImageView = UIImageView()
Expand Down Expand Up @@ -58,6 +60,6 @@ extension PosterAndTitleCell: PosterCellView {
}

public func showPosterImage(with url: URL) {
posterImageView.kf.setImage(with: url)
imageTask = posterImageView.kf.setImage(with: url)
}
}

0 comments on commit 2936cc6

Please sign in to comment.