需要 iOS 核心数据架构技巧 [英] iOS Core Data Architecture tips wanted

查看:33
本文介绍了需要 iOS 核心数据架构技巧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想就构建我的第一个 Core Data 应用程序的最佳方式以及我将需要的主要对象和交互获得一些建议.

I just want to get a few pointers on the best way to architect my first Core Data app, and the main objects and interactions I will require.

数据是远程存储的,我需要通过 HTTP 访问它,它会以 JSON 格式响应.我想使用 Core Data 在设备上缓存它.服务器上每天都会有新数据,所以我需要访问它并相应地更新模型.

The data is stored remotely and I will need to access it via HTTP and it will respond in JSON format. I want to cache this on the device using Core Data. Each day there will be new data on the server, so I need to access this and update the Model accordingly.

是否有任何 SDK 类可以帮助我解决这个问题,还是我要亲自动手?

Are there any SDK classes I can use to help me with this, or am I going to hand roll it?

我想我正在查看我调用以获取数据的模型控制器,它将返回核心数据缓存数据,并可能对 Web 服务进行后台调用以获取最新数据,然后通知视图有新数据.当我以 JSON 格式从 Web 服务获取数据时 - 我需要将其映射到 ManagedObjects 并添加到我的核心数据上下文中.

I guess I'm looking at a Model Controller that I call to get the data, it will return the core data cached data and maybe make a background call to the web service to get latest data too and then notify the view that there is new data. When I get the data from web service in JSON format - i will need to map this to ManagedObjects an add to my core data context.

推荐答案

感谢 dtuckernet,这就是我所做的 - 从大量来源收集信息,我认为这是最好的解决方案.任何人都可以随意批评(建设性的)......

Thanks dtuckernet, here is what I did do - gathering info from lots of sources, which I believe is the best solution. Anyone feel free to criticise (constructively)....

  1. 我在 CoreDataStack.h 中有我的 Core Data 堆栈(单例) - 并非完全必要,但它使我的应用委托类变得整洁.
  2. 我有一个基本的 CoreDataBackedTableViewController : UITableViewController
  3. 我的每个表视图屏幕都扩展了 CoreDataBackedTableViewController 并有一个指向 ModelController 类的 ivar.
  4. 一个示例 ModelController 类有一个 - (NSFetchedResultsController *) getData 方法,它构造 NSFetchedResultsController(也保留对它的引用)并将其返回给视图控制器,该视图控制器也将它存储在 CoreDataBackedTableViewController(它侦听更新和编辑到数据).拥有 ModelController 类允许我封装我的数据访问,以便可能有 2 个不同的视图控制器使用它(可能是 iPhone 和 iPad)
  5. 在 getData 中 - 我异步调用我的后端网络服务.使用委托进行回调
  6. 后端使用 SBJSON 进行解析,并使用 NSHttpConnection 和手工制作的 HttpService 类.
  7. 当后端返回数据时,它调用更新核心数据的 ModelController 上的委托 - 我的 fetchedResultsController 知道这一点并自动更新我的界面!这有多酷 - 我没有付出太多努力.我必须对我之前是否已经下载过数据进行一些检测,以避免重复.
  8. 准备将其推广到我应用的其余部分......

如果有人想对任何步骤进行澄清,请告诉我.

If anyone wants any clarification on any of the steps, just let me know.

这篇关于需要 iOS 核心数据架构技巧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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