Я начинаю использовать ReactiveCocoa с Swift в первый раз. Я создаю приложение, показывающее список фильмов, и я использую шаблон MVVM. Моя ViewModel выглядит следующим образом:Загрузка изображения Async с помощью ReactiveCocoa (4.2.1) и Swift
class HomeViewModel {
let title:MutableProperty<String> = MutableProperty("")
let description:MutableProperty<String> = MutableProperty("")
var image:MutableProperty<UIImage?> = MutableProperty(nil)
private var movie:Movie
init (withMovie movie:Movie) {
self.movie = movie
title.value = movie.headline
description.value = movie.description
Alamofire.request(.GET, movie.pictureURL)
.responseImage { response in
if let image = response.result.value {
print("image downloaded: \(image)")
self.image.value = image
}
}
}
}
, и я хотел бы настроить свои ячейки в UITableView, как это:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MovieCell", forIndexPath: indexPath) as! MovieCell
let movie:Movie = movieList[indexPath.row]
let vm = HomeViewModel(withMovie: movie)
// fill cell with data
vm.title.producer.startWithNext { (newValue) in
cell.titleLabel.text = newValue
}
vm.description.producer.startWithNext { (newValue) in
cell.descriptioLabel.text = newValue
}
vm.image.producer.startWithNext { (newValue) in
if let newValue = newValue {
cell.imageView?.image = newValue as UIImage
}
}
return cell
}
Является ли это правильный подход к Reactive какао? Должен ли я объявлять название и описание как Mutable или просто изображение (единственное изменение). Я думаю, что могу использовать привязку, но я не уверен, как действовать дальше.