我正在使用 RAC4 开发一个应用程序,它从服务器获取城市对象列表并将它们作为 JSON 返回。我通过将每个城市及其适当的属性存储为 City 对象来处理响应。然后,我将每个 City 映射到 CityViewModel 类型,并将 [CityViewModel] 类型的数组存储为 MutableProperty。从这里开始,每个城市都被归档到一个 tableViewCell 中,并在单元格内使用名称和下载进度条显示。点击时,单元格会触发另一个服务器请求,使用城市 nid(城市 ID)作为参数来下载包含图像等的大型 .zip 文件。
这里的目标是通过实时更新进度来为进度条设置动画。点击时,单元downloadCityData(nid: Int)
格调用启动一切的函数。
问题是,虽然城市的属性正在更新,但城市并没有通知它的侦听器对象MutableProperty<[CityViewModel]>
之外的任何变化。DataViewModel
(在这种情况下是DetailViewController
)
视图控制器:
class DetailViewController: UIViewController {
@IBOutlet weak var cityTableView: UITableView!
private var bindingHelper: TableViewBindingHelper<CityViewModel>!
var viewModel: DetailViewModel?
override func viewDidLoad() {
super.viewDidLoad()
self.viewModel = DetailViewModel()
self.viewModel!.cities.producer
.startOn(UIScheduler())
.startWithNext{ x in
/// this doesn't hear any changes as progress updates
}
bindingHelper = TableViewBindingHelper(tableView: cityTableView, sourceSignal: self.viewModel!.cities.producer, nibName: "CityCell")
}
}
视图模型:
class DetailViewModel: NSObject {
var dataManager: DataManager
let cities = MutableProperty<[CityViewModel]>([CityViewModel]())
override init() {
self.dataManager = DataManager()
super.init()
self.dataManager.progressMarker.producer
.observeOn(UIScheduler())
.startWithNext{ [weak self] (nid, progress) in
self!.cities.value = (self!.cities.value.map{ city in
if city.nid.value == nid {
print(nid, progress)
city.downloading.value = true
city.progress.value = progress
}
return city
})
}
}
func downloadCityData(nid: Int) -> SignalProducer<(JSON?, Float), NSError> {
return dataManager.getCityData(nid)
.on(next: { (json, progress) in
/// download complete
print("download complete")
self.cities.value = (self.cities.value.map{ city in
if city.nid.value == nid {
city.downloading.value = false
city.downloadedBool.value = true
city.upToDate.value = true
}
return city
})
})
}
}
数据管理器:
class DataManager: NSObject {
private let restClient = RestClient()
let progressMarker = MutableProperty<(Int, Float)>(0, 0)
override init() {
super.init()
}
func getCityData(nid: Int) -> SignalProducer<(JSON?, Float), NSError> {
return restClient.fetchCityData(nid)
.filter{ (fileName, progress) in
self.progressMarker.value = (nid, progress)
return fileName.characters.count > 0
}
.flatMap(FlattenStrategy.Latest, transform: unzipCityData)
.flatMap(FlattenStrategy.Latest, transform: unpackCityData)
}
}
城市景观模型:
class CityViewModel: NSObject {
private let city: City
let name: ConstantProperty<String>
let nid: ConstantProperty<Int>
let progress: MutableProperty<Float>
let downloading: MutableProperty<Bool>
let downloadedBool: MutableProperty<Bool>
let downloadedString: MutableProperty<String>
let upToDate: MutableProperty<Bool>
init(city: City) {
self.city = city
name = ConstantProperty(city.name)
nid = ConstantProperty(city.nid)
progress = MutableProperty(city.progress)
downloading = MutableProperty(city.downloading)
downloadedBool = MutableProperty(city.downloaded)
downloadedString = MutableProperty(city.downloaded ? "downloaded" : "download now")
upToDate = MutableProperty(city.upToDate)
super.init()
}
}
城市:
struct City {
var name: String
var nid: Int
var timestamp: Int
var progress: Float
var downloading: Bool
var downloaded: Bool
var upToDate: Bool
init() {
name = ""
nid = 0
timestamp = 0
progress = 0
downloading = false
downloaded = false
upToDate = false
}
}