2

I have A parentView which holds two different CollectionViwes. Each view is getting populated from dataBase by passing data to Observable using BehaviorRelay. Everything is working fine until I have to update Views in collection_view_2 from collection_view_1. What I am doing is passing BehaviorRelay fetched data as I am doing previously in same ParentView. But what I see is this time on tap of collection_view_1's cell the Observable of StudentCell does not get called. I am unable to figure out why its doing like that. I have seen other SO questions but no one have answer to fix this. Have a look on the code down below for better understanding.

Collection_view_1

class ClassTabCV: UIViewController, UICollectionViewDelegateFlowLayout {
    let classCells = BehaviorRelay<[ClassModel]>(value: [])
    let pickupVC = PickUpViewController()

    override func viewDidLoad() {
        super.viewDidLoad()
        classTab.rx.setDelegate(self).disposed(by: disposeBag)
        setupBinding()
    }

    func setupBinding() {
        classTab.register(UINib(nibName: "ClassTabCVCell", bundle: nil), forCellWithReuseIdentifier: "classTabCV")
        //Cell creation
        classCells.asObservable().debug("Class View: ").bind(to: classTab.rx.items(cellIdentifier: "classTabCV", cellType: ClassTabCVCell.self)) {[weak self]
            (row , element, cell) in
            cell.viewModel = element
            }.disposed(by: disposeBag)

        // item selection with model details.
        Observable
            .zip(
                classTab
                    .rx
                    .itemSelected,
                classTab
                    .rx
                    .modelSelected(ClassModel.self))
            .bind { [weak self] indexPath, model in
                self?.pickupVC.getStudentsData2(id: model.classId) // here I am trying to pass reference to update Collection_view_2 views.

            }.disposed(by: disposeBag)
    }
}

Collection_view_2

class StudentCV: UIViewController, UICollectionViewDelegateFlowLayout {


    let studentCells = BehaviorRelay<[StudentModel]>(value: [])
    var dataSource = RxCollectionViewSectionedAnimatedDataSource<SectionViewModel>(configureCell: {( _,_,_,_ ) in
        fatalError()
    })
    private let disposeBag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()

        studentsView.rx.setDelegate(self).disposed(by: disposeBag)
        setupBinding()
    }

    func setupBinding() {

        dataSource.configureCell = { (ds, cv, ip, item) in

            let cell = cv.dequeueReusableCell(withReuseIdentifier: "studentCV", for: ip) as! StudentCVCell
            cell.viewModel = item
            return cell
        }
            studentCells
                .asObservable()
                .debug("Student View: ")
                .map({ [SectionViewModel(header: "", items: $0.filter({$0.pickupStatus == 2}) ), SectionViewModel(header: "", items: $0.filter({$0.pickupStatus == 3}) )] })
                .bind(to: studentsView.rx.items(dataSource: dataSource))
                .disposed(by: disposeBag)
    }
}

ParentView_Controller

    let studentCells = BehaviorRelay<[StudentModel]>(value: [])
    let classCells = BehaviorRelay<[ClassModel]>(value: [])

    var studentCell : Observable<[StudentModel]> {
        return studentCells.asObservable().debug("IS DATA IN: ")
    } // this observable is not calling the time I pass reference from Collection_View_1 on tap.
    var classCell : Observable<[ClassModel]> {
        return classCells.asObservable()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        getAssignedClassData(classId: id)
        self.getStudentsData(id: id)
        bindViewModel()
    }

    func getStudentsData(id: Int) {


    let studentsData = Database.singleton.fetchStudents(byCLassId: id)
                self!.studentCells.accept(Array(studentsData))

        let classData = Database.singleton.fetchClass()
                self!.classCells.accept(Array(classData))

    }
    func getStudentsData2(id: Int) {

        let studentsData = Database.singleton.fetchStudents(byCLassId: id)
                self!.studentCells.accept(Array(studentsData)) // when this calls, observer is not emitting anything. 

    }

    func bindViewModel() {
        self
            .studentCell
            .asObservable()
            .debug("BIND: ")
            .observeOn(MainScheduler.instance)
            .bind(to: studentsViewController.studentCells)
            .disposed(by: disposeBag)

        self
            .classCell
            .asObservable()
            .debug("CELL BIND: ")
            .observeOn(MainScheduler.instance)
            .bind(to: classViewController.classCells)
            .disposed(by: disposeBag)
    }

On tap of collection_view_1. Collection_view_2 should update data. and change views accordingly. I am doing same thing in another of my view, but the difference is. that collectionView is getting change reference from a buttonClick not from any other CollectionView. Any help regrading my issue will be appreciated. Thanks

Chalenger
  • 65
  • 1
  • 12

0 Answers0