使用现有(普通)模型将核心数据添加到 iPhone 应用程序

发布于 2024-11-28 21:36:26 字数 692 浏览 0 评论 0原文

我有一个相当复杂的 iPhone 应用程序,它依赖 API 从服务器获取数据并将其显示给用户。我有大约 5 个模型类在整个应用程序中使用 - 它们只是扩展 NSObject。

我想为模型添加一些持久性,以允许即使设备处于离线状态也可以使用应用程序的某些部分 - 它基本上只是美化的缓存。我只希望保留模型的某些实例 - 例如,用户已添加书签的项目 - 而其他实例则不应该保留,例如数百个搜索结果。

Core Data 是正确的解决方案吗?我看到的困难是:

  • 我必须改变在整个项目中实例化模型对象的方式。我必须将它们初始化为上下文的一部分,如果它们实际上来自外部 API,则不一定有意义。
  • 我需要小心不要保留我不想要的实例。这似乎归结为要么在创建托管对象后立即删除它(真的很尴尬),要么使用单独的非持久上下文来处理我不想持久化的实例(更好,但仍然有点尴尬)

我希望我可以保留在整个应用程序中使用我的模型而不更改不需要关心持久性的代码,但考虑到我的要求,这似乎不可行。另一种方法是与现有对象并行设置一组新的托管对象,并且仅将托管对象用于持久性 - 但这种重复似乎从来都不是正确的解决方案。

我应该尝试将核心数据硬塞进去吗?如果是,该怎么做?或者我应该只是看看其他选项 - sqlite3 (对于我需要的东西来说似乎有点复杂),用户默认值(可能不是他们想要的),甚至序列化并将我自己的对象写入文件(看起来很黑客) )。

感谢您的任何意见!

I have a reasonably complex iPhone app that relies on an API to fetch data from a server and display it to the user. I have about 5 model classes that are used throughout the app - they simply extend NSObject.

I want to add some persistance to the models, to allow certain parts of the app to be used even if the device is offline - it really is basically just glorified caching. I only want certain instances of my models to be persisted - for example, items the user has bookmarked - and others should not, for example hundreds of search results.

Is Core Data the right solution for this? The difficulties I can see are:

  • I would have to change the way I instantiate my model objects throughout the project. I would have to initialize them as part of a context, which does not necessarily make sense if they're actually coming from an external API.
  • I would need to be careful not to persist instances I don't want. This seems to boil down to either deleting a Managed Object right after it's created (really awkward) or using a separate, non-persistent context for instances I don't want persisted (better, but still somewhat awkward)

I was hoping I could keep using my models throughout the app without changing the code that doesn't need to care about persistance, but that doesn't seem feasible given my requirements. The alternative is to set up a new set of Managed Objects in parallel to my existing objects, and only use the Managed Objects for persistance - but this kind of duplication never seems like the right solution.

Should I be trying to shoehorn Core Data into this, and if so, how? Or should I just be looking at other options - sqlite3 (seems a bit complicated for what I need), user defaults (probably not what they're intended for), or even serializing and writing my own objects to files (seems hack-ish).

Thanks for any input!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巨坚强 2024-12-05 21:36:26

为什么不看看 NSKeyedArciver/Unarchiver 呢?我想说,当处理大量数据和 sqlite 数据库时,Core Data 是显而易见的解决方案,但 NSKeyedArchiver 是我用来保存模型的工具。
查看 Apple 的文档此处< /a>.在我看来,使用 NSKeyedArchiver 非常简单,您可以用它存储几乎所有您需要的东西。

Why not take a look at NSKeyedArciver/Unarchiver? I would say that Core Data is the obvious solution when working with very large amounts of data along with sqlite databases, but NSKeyedArchiver is what I use to save models.
Check out Apple's documentation here. Using NSKeyedArchiver is pretty simple IMO, and you can store pretty much anything you need with it.

伏妖词 2024-12-05 21:36:26

现在,您将数据对象实例化为 NSObject 的子类。您可以使用另一个间接级别来选择是否创建一个对象作为 ManagedObjectModel 的子类,并使用定义用于访问这些对象信息的接口的协议来保持应用程序的大部分内容不变,然后传递 < 类型的对象代码>id。无法获取将表明存在非本地对象。

或者,您可以将所有内容创建为托管对象,并且除了您不想在本地存储的数据的密钥之外不存储任何内容。因此,您可以知道是否存在任何对象以及如何在需要时获取它,但您不会花费大量时间或空间来保留应保留在云中的数据。执行此操作的方法是将模型中除键之外的所有内容定义为可选。

我会选择 Core Data,因为它的灵活性、实用性和优化性。确实值得学习。我不会推荐 sqlite3 方法,iOS 上的前进方向是 Core Data。如果您使用某种文件存储,您应该考虑如何批量读取和/或写入,因为在执行多个小文件操作时可能会遇到一些糟糕的性能问题。

Right now you instantiate data objects as subclasses of NSObject. You could use another level of indirection to choose whether to create an object as a subclass of ManagedObjectModel instead, and keep most of your app untouched using a protocol defining the interface you use to access information from those objects, then passing around objects of type id<DataObjectProtocol>. Failure to fetch would be an indication of a non-local object.

Alternatively you could create everything as a managed object and just not store anything but a key for the data you don't want to store locally. Thus you can know if any object exists and how to fetch it if needed but you don't spend a lot of time or space persisting data that should remain in the cloud. The way to do this would be to define all but the key in the model as optional.

I would choose Core Data because of its flexibility, utility and optimisation. It really is worth learning. I would not recommend the sqlite3 approach, the way forward on iOS is Core Data. If you use some kind of file storage you should think about how to batch reads and/or writes since you could hit some bad performance issues doing multiple small file operations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文