gpt4 book ai didi

ios - RAC MutableProperty Producer 不向订阅者发送事件

转载 作者:行者123 更新时间:2023-11-28 06:42:29 24 4
gpt4 key购买 nike

我正在使用 RAC4 开发一个应用程序,它从服务器获取城市对象列表并将它们作为 JSON 返回。我通过将每个城市及其适当的属性存储为 City 对象来处理响应。然后,我将每个城市映射到 CityViewModel 类型,并将 [CityViewModel] 类型的数组存储为 MutableProperty。从这里每个城市都被归档到一个 tableViewCell 中,并在单元格中使用名称和下载进度条显示。点击时,单元会触发另一个服务器请求,使用城市 nid(城市 ID)作为参数来下载包含图像等的大型 .zip 文件。

此处的目标是通过进度的实时更新为进度条设置动画。点击时,单元格调用 downloadCityData(nid: Int)启动一切的功能。

问题是,在更新城市属性的同时,城市 MutableProperty<[CityViewModel]>DataViewModel 之外没有通知它的听众任何变化目的。 (在这种情况下是 DetailViewController )

View Controller :

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")

}
}

View 模型:

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
}
}

最佳答案

我看到你正在使用 Colin Eberhardt's TableViewBindingHelper .看起来这正在剥夺 ViewController 监听和观察 viewModel 信号的能力。我之前在使用这个帮助程序类时也遇到过问题。

关于ios - RAC MutableProperty Producer 不向订阅者发送事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37575408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com