0

I am trying to update tableview via a delete button on my table cell class. But it doesn't update the tableview instantly which I don't know if it's too much to ask from behavior relay.

Properties:

private let businesses: BehaviorRelay<[StoresFeedModel]> = BehaviorRelay(value: [])

 var businessDataMain = [StoresFeedModel]()

 var businessDataFav = [StoresFeedModel]()

The button:

@objc func deleteTapped(_ sender: UIButton){

    
    deleteFavIdCoreData(id: storeid) //
 StoresFeed.shared.getFavDataforFavSegment()

   
}

The method that does the api call and updates the businessDataFav:

func getFavDataforFavSegment() {
    businesses.accept(businessDataFav)
    StoresTabCell.shared.fetchStoreIdCoreData()
    if StoresTabCell.shared.favIDsCoreData.removingDuplicates().count > 0  {
        for i in 0..<StoresTabCell.shared.favIDsCoreData.removingDuplicates().count {
            YelpAPIManager.shared.getFavStoreInfo(id: StoresTabCell.shared.favIDsCoreData.removingDuplicates()[i].favoriteStoreID!) { dataFav in
                self.businessDataFav = dataFav
            }
        }
    } else {
        businessDataFav = [StoresFeedModel(title: "No Favorites Yet", image: noValueImage, id: "No id yet")]
        return
    }
}

Binding method:

func bindTableViewMain() {
    businesses.asObservable()
        .bind(to: tableView
                .rx
                .items(cellIdentifier: storesFeedCellId, cellType: StoresTabCell.self)
        )
    {
        row, businessData, cell in

        cell.configureWithData(dataModel: businessData)
    }
        .disposed(by: disposeBag)
}

I have two segments on the main view:

@objc func actionofSC() {
    let type = segments[segmentControl.selectedSegmentIndex]
    switch type {
    case .allStores:
    getMainData()

    case .favorites:
    getFavDataforFavSegment()
        
    }
}

Now what I want is the update of TableView while I am browsing on favorites segment and hit the delete button. I am keeping the favorite ids on core data. So the delete or fav buttons actually first updates the core data entity and then I am doing the api call via those ids. What am I doing wrong?

iPancake
  • 109
  • 1
  • 9

0 Answers0