Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxSwift reload tableview

let officialAccountObservable : Observable<[SearchUser]> = SearchAPI.sharedAPI.suggestAccounts()

        officialAccountObservable.bind(to: tableView.rx.items(cellIdentifier: "followcell", cellType: FollowCell.self)) {
            (index, user , cell) in
            if user.profileImagePath.isEmpty == false {
                cell.profile.af_setImage(withURL: URL.init(string: user.profileImagePath)!)
            }else {
                cell.profile.image = UIImage.init(named: "icon_user_03")
            }
            cell.nickName.text = user.nickName

            cell.follow.rx.tap
                .debounce(0.3, scheduler: MainScheduler.instance)
                .subscribe(onNext: {
                    [unowned self] in
                    cell.setFollow(user: user, completion: { (result) in
                        if(result == true){

                        }
                    })
                }).addDisposableTo(self.disposeBag)
        }.addDisposableTo(disposeBag)

func suggestAccounts() -> Observable<[SearchUser]> {

    let uri = Constants.VyrlSearchURL.suggestUsers

    return Observable.create { observer in
        let request = Alamofire.request(uri, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: Constants.VyrlAPIConstants.getHeader()).responseArray { (response: DataResponse<[SearchUser]>) in
            let value = response.result.value

            observer.onNext(value!)
            observer.onCompleted()
        }

        return Disposables.create(with: request.cancel)
    }
}

I want to reload the table view in code (result == true) For reload, OfficialAccountObservable must be received. My code is all over and I wonder how I can update it in that state.

like image 741
kmb Avatar asked Mar 08 '23 17:03

kmb


1 Answers

AFAIK, this is the standard way of doing this -

let source = PublishSubject<Observable<[SearchUser]>>()
let officialAccountObservable: Observable<[SearchUser]> = source.switchLatest()
source.onNext(suggestAccounts()) // every call will refresh your table
like image 166
Maxim Volgin Avatar answered Mar 15 '23 14:03

Maxim Volgin