Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC, can model save/load its data?

Quick question, my data model is a singleton object and it contains a list of names that I want to archive. My idea is to make the model responsible for loading and saving it's own data. The model's load method will be called by the ViewController's viewDidLoad method and save by the ViewController's applicationWillTerminate. I could do the load/save directly within the ViewController, but this would be messy as the list of names are an instance variable of the model.

gary

like image 602
fuzzygoat Avatar asked Mar 14 '26 13:03

fuzzygoat


2 Answers

You could just load and save in the init and dealloc methods (although it's common to call a save method explicitly). It's a good idea to encapsulate it within the model class. If you're loading from the network you might want to have a separate loadData method or something, rather than doing it from init.

like image 62
Felixyz Avatar answered Mar 16 '26 04:03

Felixyz


Apple recommends using lazy initialization wherever possible, so I think you're heading down the right path, though you might want to consider making the method name something that looks like a property accessor, e.g. -names rather than -load (especially since there's a class method named +load that means something quite different).

like image 20
jlehr Avatar answered Mar 16 '26 02:03

jlehr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!