在 iPhone Core Data 或 SQLite 中存储长期数据的最佳方式是什么?

发布于 2024-09-06 05:42:42 字数 187 浏览 1 评论 0原文

我正在开发针对 3.1.3 及更高版本 SDK 的 iPhone 应用程序。我想知道在 iPhone 上存储用户长期数据而不损失性能、一致性和安全性的最佳方法。

我知道,我可以使用 Core Data、PList 和 SQL-Lite 以自定义格式存储用户特定数据。但是,想知道在不久的将来,哪一个最好使用,而不会影响应用程序的性能和可扩展性。

I am working on iPhone app targeting 3.1.3 and later SDK. I want to know the best way to store user's long term data on iphone without losing performance, consistency and security.

I know, that I can use Core Data, PList and SQL-Lite for storing user specific data in custom formats.But, want to know which one is good to use without compromising app performance and scalability in near future.

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

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

发布评论

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

评论(2

无法回应 2024-09-13 05:42:42

这取决于。 “用户数据”一词涵盖了广泛的规模、复杂性和用途,每种数据都有不同的最佳存储策略。

(1) 如果大小和复杂性较低,并且用途主要是控制应用程序本身,请使用 NSUserDefaults 将数据存储在用户默认值中。

(2) 如果大小很小并且复杂性可以通过数组、字典等来管理,则存储在 plist 中。大小很重要,因为存储在 plist 中的所有数据都以一个块的形式加载到内存中。

(3) 如果规模很大但复杂度较低,例如索引卡系统等大量模板记录,则使用直接SQL。 SQL 可以更快地在非常大的数据库中查找和保存简单和重复的信息。

(4) 如果复杂度很高,无论大小,都使用Core Data。 Core Data 专为管理复杂信息而设计。如果大小较小,请使用 xml 存储。如果它很大,请使用 SQL 存储。

随着我对 Core Data 的熟悉,我发现自己几乎将它用于除用户默认值之外的所有内容。它的学习曲线很陡峭,但一旦掌握了它,您就拥有了一个强大且易于使用的工具来管理应用程序数据。我可能会在它不是最佳的情况下使用它,只是因为它加快了开发时间。

It depends. The term "user data" covers a wide scale in size, complexity and usage each of which have different optimal storage strategies.

(1) If size and complexity are low and the usage is primarily controlling the app itself, store the data in the user defaults using NSUserDefaults.

(2) If the size is small and the complexity can be managed by arrays, dictionaries etc then store in a plist. Size counts because all the data stored in a plist is loaded into memory in one chunk.

(3) If the size is very large but the complexity is low e.g. a large number of template records such as an index card system, then use direct SQL. SQL is faster for finding and saving simple and repetitive information in a very large DB.

(4) If the complexity is very high, use Core Data regardless of size. Core Data is specifically designed to manage complex information. If the size is small, use an xml store. If it is large, use a SQL store.

As I gained familiarity with Core Data, I find myself using it for almost everything except user defaults. It has a steep learning curve but once you master it, you have a powerful and easy to use tool for managing application data. I probably use it in situations where it is not optimal just because it speeds development time.

埋葬我深情 2024-09-13 05:42:42

我不同意 TechZen 的名单。如果您正在处理旧版 SQLite 数据库,则只有 3 号才是正确答案。没有理由选择原始 sqlite 而不是 Core Data。 Core Data 几乎在所有情况下都会表现得更好,并且会大量减少您必须编写的代码量。

另外,我要警告不要使用 plist。它们的读写成本很高,而 Core Data 数据库几乎在所有情况下都优于它们。

至于使用 Core Data,除非在最极端的情况下,否则您应该始终使用 SQLite 后端(XML 在 iOS 上不可用)。

简而言之,如果您要保存单个值,请将其存储在 NSUserDefaults 中。

否则使用核心数据。

更新

目前,使用比原始 SQLite 性能更高的 Core Data 无法完成一件事。这就是更新数万行中单个列的值的能力。这是因为要修改一行,Core Data 会将该行加载到内存中,然后再次将其写回。

对于所有其他情况,与编写自己的访问器、对象并处理内存及其生命周期相比,使用 Core Data 可以获得更好的性能。

Core Data 将胜过您自己编写的任何数据访问器,并且它将以比您更好的方式处理底层文件的写入和读取。为什么要重新发明轮子?

I have to disagree on TechZen's list. Number 3 is only a right answer if you are dealing with a legacy SQLite database. There is never a reason to choose raw sqlite over Core Data. Core Data will perform better in nearly every situation and will reduce the amount of code you have to write by a significant amount.

In addition, I would caution against using plists. They are expensive to read and write and a Core Data database will outperform them in nearly every situation.

As for using Core Data, you should always use a SQLite back-end (XML is not available on iOS) except in the most extreme circumstances.

In short, if you are saving a single value, store it in NSUserDefaults.

Otherwise use Core Data.

Update

There is one SINGLE thing that cannot be done with Core Data more performant than raw SQLite currently. That is the ability to update a single column's values across tens of thousands of rows. That is because to modify a row, Core Data loads that row into memory and then writes it back out again.

For every other situation you are going to gain better performance with Core Data than you are writing your own accessors, objects and dealing with the memory and lifecycles thereof.

Core Data will outperform any data accessors that you are going to write yourself and it is going to handle writing to and reading from the underlying file in a better manner than you are. Why re-invent the wheel?

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