Gabriel Bastos
Gabriel Bastos

Reputation: 570

Lifecycle in MvvmCross

I have an Mvvmcross app that has a TableViewController in root. Each row opens a DetailViewController. And inside each one of it, you can edit it in a EditViewController.

So I have a ViewModel for each view. I'm dealing with two problems here:

1 ) In DetailViewController i subscribe it to a database message. When i close it, i gotta dispose this subscribeToken. So i would need to call this when DetailViewController got destroyed. But cant call it when it disappears, because when I open editViewController it will send a message that DetailViewController gotta listen to.

So I cannot dispose it in ViewDidDisappear method. But the other option would be in ViewDidUnload. But this method is only called in MemoryWarnings. So it is not disposing the token. That is not good .

2) The other problem is: for each DetailsViewcontroller that i open, i have to save in Settings what is the current id. and then when I leave, i have to remove it from Settings. So the same problem here. If i remove it in ViewDidDisappear it will remove when i'm in EditViewController, and i cant, it gotta be set there. But then if i remove only in ViewDidUnload it will not be called, and this variable must be removed.

When should I call the OnDestroy method to both cases?

In Android i'm calling in OnDestroy. Where should i call it in iOS?

Thanks in regards,

Upvotes: 3

Views: 269

Answers (1)

Stuart
Stuart

Reputation: 66882

ViewDidUnload is not an option - it's deprecated and won't be called (since a long time ago - e.g. maybe since iOS5?).

iOS doesn't really provide a general ViewController override for when the ViewController is "no longer used". However, if you have control of the ViewControllers in your app - e.g. if you are using a NavigationController which never reuses ViewControllers after they have been popped - then it should be relatively straight-forward to provide your own "cleanup" method and to call it from your own navigation control logic - e.g. from a custom presenter using events generated by a NavigationController.

Upvotes: 4

Related Questions