MVC,模型可以保存/加载其数据吗? [英] MVC, can model save/load its data?

查看:76
本文介绍了MVC,模型可以保存/加载其数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题,我的数据模型是一个单例对象,它包含我要存档的名称列表.我的想法是使模型负责加载和保存自己的数据.模型的load方法将由ViewControllerviewDidLoad方法调用,而save将由ViewControllerapplicationWillTerminate调用.我可以直接在ViewController中进行加载/保存,但这很麻烦,因为名称列表是模型的实例变量.

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.

加里

推荐答案

您可以只加载并保存init和dealloc方法(尽管通常显式调用save方法).将其封装在模型类中是一个好主意.如果您是从网络上加载,则可能希望使用单独的loadData方法或其他方法,而不是通过init进行操作.

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.

这篇关于MVC,模型可以保存/加载其数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆