当我们使用Core Data时,我们应该创建模型类吗? [英] Should we create Model Classes when we use Core Data?

查看:158
本文介绍了当我们使用Core Data时,我们应该创建模型类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iPad应用程式,如果使用者没有网际网路存取权,而且稍后与后端资料库同步处理,则需要我们在本机储存资料。



对于本地存储,我计划使用Core Data与SQLite。



我第一次使用Core Data,似乎它检索实体和存储



那么我应该创建一个Model类吗?



什么是一个好的设计为这样的应用程序。



我有一个DataEngine类,其责任是存储实体在服务器或本地DB基于连接。



现在我很困惑如果我需要创建一个Model类,并要求单个模型类使用NSMangaedObjectContext与字典表示保存自己或者直接保存数据,而不是创建一个模型对象,并要求它



我应该为每个实体使用Moel类,并且将服务器作为从/到服务器的JSON表示之间的接口。



或者我应该完全依赖Core Data创建的实体和关系舰船吗?

解决方案

我会向后做:先检查一些东西,然后检查一些想法。



检查我自己的问题。 p>

我会说在你的自定义类别上,你可以做JSON表示和你的类之间的接口。



您还应该查看 RestKit ,它可以完成您所需的大部分工作。



b b b >根据连接性将本地数据同步到服务器;
  • 使用模型类。



  • 1



    我想你应该有一个具有通用代码的类,每个模型类都应该有自己的映射(在模型和JSON之间映射)和保存方法。



    另一个类,可能是您的DataEngine类,负责在正确的时间保存正确的对象。
    查看 RestKit ,它有助于映射和保存。我不确定同步。



    问题2



    我认为你应该有模型类。



    对于这个 my question 可能对您有用,因为您可以创建一个带有生成的类文件的CoreData模型,并在需要时更新它保留您的自定义代码。


    I am working on an iPad application, that requires me to store data locally if the user doesn't have internet access and later on sync with the back-end database.

    For local storage, I am planning to use Core Data with SQLite.

    I am using Core Data for the first time and it seems, it retrieves entity and store entity in the form of a dictionary.

    So should I be creating Model classes at all ?

    What is a good design for such application.

    I have a DataEngine class whose responsibility is to store entity on a server or local DB based on connectivity.

    Now I am little confused If I need to create a Model class and ask individual model classes to save themselves using NSMangaedObjectContext with a dictionary representation Or just directly save data instead of creating a model object and asking it to do it ?

    Should I be using a Moel class for each entity and that will server as interface between JSON representation that coms/goes from/to server.

    and the representation I get from managedObjectContext.

    Or shall I compeletely rely on the entity and relation ships that Core Data creates ?

    解决方案

    I'll do this backwards: first some things for you to check and then some ideas.

    Check my own question.

    I'd say that on your custom categories you can do the interface between the JSON representation and your classes.

    You should also check RestKit which can do already much of what you need.


    You're talking about two separate problems as far as I can understand:

    1. Syncing local data to the server based on connectivity;
    2. Using model classes.

    Problem 1

    I think you should have a class with the common code and each of your model classes should have its own mapping (to map between model and JSON) and saving methods.

    Another class, that may be your DataEngine class, takes care of saving the right objects at the right time. Take a look at RestKit as it helps with the mapping and the saving. I'm not sure about the syncing though.

    Problem 2

    I think you should have model classes. It helps a lot to work with objects and you have then a place to save methods for finding different kinds of data.

    For this my question might be useful for you because you can create a CoreData model with generated class files and update it whenever you want while keeping your custom code.

    这篇关于当我们使用Core Data时,我们应该创建模型类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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