Chapter 24 : action.inputs vs action.execute for onUpdate Action

Hello there, I’m looking for implement a custom solution of the onUpdate Action in the EditTaskViewModel with an object as parameters, instead of passing a string.
To me, the logical way was to do something like this in the EditViewController :

let currentplayer = Observable.combineLatest(nameTextField.rx.text.asObservable(), lastNameTextField.rx.text.asObservable()) { name, lastName -> Player? in
            
            guard let name = name, let lastName = lastName,
                name.characters.count > 1, lastName.characters.count > 0 else {
                    return nil
            }
            let player = Player()
            player.name = name
            player.lastName = lastName
            return player
            }
            .shareReplay(1)

okButton.rx.tap
      .withLatestFrom(currentplayer)
      .subscribe(viewModel.onUpdate.inputs)
      .addDisposableTo(rx_disposeBag) 

But I’ve an error message which said :

Here is my EditPlayerViewModel, where I modified the onUpdate Action to take a Player as first parameter, instead of a String in the original version :

import Foundation
import RxSwift
import Action
import NSObject_Rx

struct EditPlayerViewModel {
    var player: Player
    let onUpdate: Action<Player, Void>
    let onCancel: CocoaAction!
    let disposeBag = DisposeBag()
    
    init(player: Player,
         coordinator: SceneCoordinatorType,
         updateAction: Action<Player, Void>,
         cancelAction: CocoaAction? = nil) {
        
        self.player = player
        onUpdate = updateAction
        
        onCancel = CocoaAction {
            if let cancelAction = cancelAction {
                cancelAction.execute()
            }
            return coordinator.pop()
        }
        
        onUpdate.executionObservables
            .take(1)
            .subscribe(onNext: { _ in
                coordinator.pop()
            })
            .addDisposableTo(disposeBag)
    }
} 

The only solution I found was to use the .execute instead of .inputs on the onUpdate method from the viewModel :

okButton.rx.tap
            .withLatestFrom(currentplayer)
            .subscribe(onNext: {[weak self] player in
                if let mplayer = player {
                    self?.viewModel.onUpdate.execute(mplayer)
                }
            })
            .addDisposableTo(rx_disposeBag)

Do you think it’s the good solution or did I forget something?

Regards :slight_smile:

Well, I found the solution… :smiley:

func bindViewModel() {
        nameTextField.text = viewModel.player.name
        lastNameTextField.text = viewModel.player.lastName
        
        cancelButton.rx.action = viewModel.onCancel
        
        let enableButton = Observable.combineLatest(nameTextField.rx.text, lastNameTextField.rx.text) { (name, lastName) -> Player in
            let player = Player()
            player.name = name ?? "Empty"
            player.lastName = lastName ?? "Empty"
            return player
        }
        
        okButton.rx.tap
            .withLatestFrom(enableButton)
            .subscribe(viewModel.onUpdate.inputs)
            .addDisposableTo(rx_disposeBag)

    }