First off, thanks for the in depth book on the topic!
Quick question, to quote from Chapter 24:
The last thing to tackle is the addition of existing items. For this, you’ll need a new Action that isn’t temporary; remember that actions have to be referenced other than via a subscription, otherwise they’ll be deallocated. As mentioned in Chapter 19, this is a frequent source of confusion.
Create a new lazy variable in TasksViewModel:
lazy var editAction…
I did refer back to Chapter 19 and couldn’t find a discussion there about when and how to use Actions in a temporary vs permanent way. Can I trouble you to elaborate a bit further on the when and how? Is that the purpose of the “lazy var”, to “use” the action outside of a subscription? Why do we want the action to be permanent in this particular example?
The problem I’m trying to solve is extending the ideas in Chapter 24 to include a login (through Realm Cloud) before viewing the task list, and in order to do that I need to pass login credentials from the LoginViewController to the LoginViewModel, and not sure if I should be pointing those credentials towards a function within LoginViewModel, towards a lazy var, or what. In the example ViewModel code, you have a combination of short functions and actions defined; when would you choose one over the other? Help would be greatly appreciated.
A concrete example would be contrasting these two:
func onCreateTask() -> CocoaAction { return CocoaAction { _ in return self.taskService .createTask(title: "") .flatMap { task -> Observable<Void> in let editViewModel = EditTaskViewModel(task: task, coordinator: self.sceneCoordinator, updateAction: self.onUpdateTitle(task: task), cancelAction: self.onDelete(task: task)) return self.sceneCoordinator .transition(to: Scene.editTask(editViewModel), type: .modal) .asObservable().map { _ in } } } } lazy var editAction: Action<TaskItem, Swift.Never> = { this in return Action { task in let editViewModel = EditTaskViewModel( task: task, coordinator: this.sceneCoordinator, updateAction: this.onUpdateTitle(task: task) ) return this.sceneCoordinator .transition(to: Scene.editTask(editViewModel), type: .modal) .asObservable() } }(self)
They both end up presenting the editing modal, but one is constructed as a lazy var and the other as a function?
Many thanks in advance,
Scott