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

时间:2016-06-01 17:39:34

标签: ios swift reactive-cocoa reactive-cocoa-4

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

此处的目标是通过实时更新进度来设置进度条的动画。在点击时,单元格调用downloadCityData(nid: Int)函数,该函数将所有内容都关闭。

问题是,虽然城市的属性正在更新,但城市MutableProperty<[CityViewModel]>并未在DataViewModel对象之外通知其侦听者任何更改。 (在这种情况下是DetailViewController

的ViewController:

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

的DataManager:

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

CityViewModel:

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

1 个答案:

答案 0 :(得分:1)

我看到您正在使用Colin Eberhardt's TableViewBindingHelper。它似乎剥夺了ViewController监听和观察viewModel信号的能力。我之前也遇到过使用这个助手类的问题。