1

I'm developing an app with RAC4 that fetches a list of city objects from a sever and returns them as JSON. I handle the response by storing each city and its appropriate properties as a City object. I then map each City into a CityViewModel type and store an array of type [CityViewModel] as a MutableProperty. From here each city is filed into a tableViewCell and displayed with a name and download progressBar inside the cell using. On tap, the cell triggers another sever request using the city nid (city ID) as a param to download a large .zip file containing images etc.

The goal here is to animate the progress bar with live updates on progress. On tap, the cell calls the downloadCityData(nid: Int) function which kicks everything off.

The issue is, while the properties of cities are getting updated, the cities MutableProperty<[CityViewModel]> is not notifying it's listeners of any changes, outside of the DataViewModel object. (which in this case is the 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")

    }
}

ViewModel:

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

City:

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
    }   
}
Michael Bopp
  • 656
  • 1
  • 4
  • 13

1 Answers1

1

I see you're using Colin Eberhardt's TableViewBindingHelper. It appears that is taking away the ViewController's ability to listen to and observe the viewModel's signals. I have had issues using this helper class before as well.

aware
  • 118
  • 1
  • 9
  • A partial answer I suppose. Would be great to pin-point exactly what is causing the issue within the `TableViewBindingHelper` class. – Michael Bopp Jun 03 '16 at 16:12