Building Netflix App in Swift 5 and UIKit - (Xcode 13, 2021) - Episode 7 - SDWebImage
func configure(with model: ContentModel) {
if let title = model.title {
nameLabel.text = title
} else if let anotherTitle = model.original_title {
nameLabel.text = anotherTitle
} else {
nameLabel.text = "Default Title"
}
guard
let imageURLString = model.poster_path,
let imageURL = URL(string: "\(TMDBConstants.imagePath.rawValue)\(imageURLString)") else { return }
let publisher: AnyPublisher<UIImage?, Never> = URLSession
.shared
.dataTaskPublisher(for: imageURL)
.compactMap({$0.data})
.compactMap({UIImage(data: $0)})
.replaceError(with: UIImage(systemName: "video"))
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
var cancellables: AnyCancellable?
cancellables = publisher
.sink(receiveCompletion: { _ in
cancellables?.cancel()
}, receiveValue: { [weak self] image in
self?.imageView.image = image
})
}
publisher
와 cancellables
사용 및 cancel()
메소드를 통한 one-shot publisher
사용법enum TMDBContentsEnum: Int, CaseIterable {
case trendingMovies = 0
case trendingTVs = 1
case popular = 2
case upcoming = 3
case topRated = 4
var headerTitle: String {
switch self {
case .trendingMovies:
return "Trending Movies"
case .trendingTVs:
return "Trending TVs"
case .popular:
return "Popular"
case .upcoming:
return "Upcoming Movies"
case .topRated:
return "Top Rated"
}
}
}