0

I'm going to show the view the data I got from the server. An error occurred while developing using Rxswift and MVVM. The id value must be received from the product model, and an error occurs in this part.

extension SelectViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        print("Tap")
        let detailVC = DetailViewController()

        detailVC.product = products[indexPath.row]
        detailVC.productId = products[indexPath.row].id

        self.navigationController?.pushViewController(detailPassVC, animated: true)
    }
}

This is the code for the part that passes the id value. detailVC.product = products[indexPath.row]An error occurs in this area. error message is "Thread 1: Fatal error: Index out of range"

    var products:[ProductModel] = []
    var product = ProductModel()
    var productId = Int()

    func initViewModel() {
        let input = DetailViewModel.Input(loadDetailData: getData.asSignal(onErrorJustReturn: ()), id: productId)
        let output = viewModel.transform(input: input)
        
        output.loadDetail.asObservable().bind(onNext: { data in
            self.infoLabel.text = data?.detailDescription
            self.passView.setData(data!)
            self.secondView.setData(data!)
            self.fareView.setData(data!)
            self.totalLabel.text = totalPrice(data!)
        }).disposed(by: disposeBag)
    }

This is the code for the part where the view model is bound. And This is my viewmodel code

class DetailViewModel: BaseViewModel {
    private let repo = ProductRepository()
    private let disposeBag = DisposeBag()
        
    struct Input {
        let loadDetailData: Signal<Void>
        let id: Int

    }
    struct Output {
        let loadDetail: Driver<DetailModel?>
        let error: Driver<String?>
    }
    
    func transform(input: Input) -> Output {
        let loadDetail = PublishSubject<DetailModel?>()
        let msg= PublishSubject<String?>()
        
        input.loadDetailData.asObservable()
            .flatMap { self.productRepo.detailProduct(input.id)}
            .subscribe(onNext: { data in
                switch data {
                case let .success(detail):
                    loadDetail.onNext(detail)
                default:
                    errorMessage.onNext("load fail")
                }
            }).disposed(by: disposeBag)
        
        return Output(loadDetail: loadDetail.asDriver(onErrorJustReturn: nil),error: msg.asDriver(onErrorJustReturn: nil))
    }
}

How can I send the ID value?

earth
  • 85
  • 3
  • 13
  • Where do you assign to `products`? – Daniel T. Nov 05 '21 at 01:26
  • @DanielT. It is assigned from the tableview delete attribute on the previous screen. ``` func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) print("Tap") let detailVC = DetailViewController() detailVC.product = products[indexPath.row] detailVC.productId = products[indexPath.row].id self.navigationController?.pushViewController(detailVC, animated: true) } ``` – earth Nov 05 '21 at 01:48
  • That's where you assign `product`. Where do you assign `products`? – Daniel T. Nov 05 '21 at 01:53
  • And there should not be any Subjects in your view model, nor should there be a DisposeBag. You don't need them. – Daniel T. Nov 05 '21 at 01:56

0 Answers0