简体   繁体   English

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

[英]What is best way to store long term data in iPhone Core Data or SQLite?

I am working on iPhone app targeting 3.1.3 and later SDK. 我正在开发定位于3.1.3和更高版本SDK的iPhone应用程序。 I want to know the best way to store user's long term data on iphone without losing performance, consistency and security. 我想知道在不损失性能,一致性和安全性的情况下在iphone上存储用户长期数据的最佳方法。

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. 我知道我可以使用Core Data,PList和SQL-Lite以自定义格式存储用户特定的数据。但是,我想知道在不损害应用性能和可伸缩性的情况下使用哪种是好的。

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. (1)如果大小和复杂度较低,并且使用主要是控制应用程序本身,请使用NSUserDefaults将数据存储在用户默认值中。

(2) If the size is small and the complexity can be managed by arrays, dictionaries etc then store in a plist. (2)如果大小较小并且可以通过数组,字典等管理复杂性,则将其存储在plist中。 Size counts because all the data stored in a plist is loaded into memory in one chunk. 大小很重要,因为存储在plist中的所有数据都以一个块的形式加载到内存中。

(3) If the size is very large but the complexity is low eg a large number of template records such as an index card system, then use direct SQL. (3)如果大小很大,但是复杂度很低,例如,大量模板记录,例如索引卡系统,则使用直接SQL。 SQL is faster for finding and saving simple and repetitive information in a very large DB. SQL在大型数据库中查找和保存简单且重复的信息的速度更快。

(4) If the complexity is very high, use Core Data regardless of size. (4)如果复杂度很高,则无论大小如何都使用Core Data。 Core Data is specifically designed to manage complex information. 核心数据专门用于管理复杂信息。 If the size is small, use an xml store. 如果大小较小,请使用xml存储。 If it is large, use a SQL store. 如果很大,请使用SQL存储。

As I gained familiarity with Core Data, I find myself using it for almost everything except user defaults. 随着我对Core Data的熟悉,我发现除了用户默认值之外,几乎所有东西都使用它。 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. 我可能会在它不是最佳的情况下使用它,因为它会加快开发时间。

I have to disagree on TechZen's list. 我不得不不同意TechZen的名单。 Number 3 is only a right answer if you are dealing with a legacy SQLite database. 如果要处理旧版SQLite数据库,则数字3仅是一个正确的答案。 There is never a reason to choose raw sqlite over Core Data. 从来没有理由选择原始sqlite而不是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. 它们的读写成本很高,并且在几乎每种情况下,Core Data数据库都将胜过它们。

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. 至于使用Core Data,除非在最极端的情况下,否则应始终使用SQLite后端(iOS上不提供XML)。

In short, if you are saving a single value, store it in NSUserDefaults . 简而言之,如果要保存单个值,请将其存储在NSUserDefaults

Otherwise use Core Data. 否则,请使用核心数据。

Update 更新资料

There is one SINGLE thing that cannot be done with Core Data more performant than raw SQLite currently. 目前,使用Core Data不能比目前的原始SQLite拥有更出色的功能。 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. 那是因为修改一行,Core Data将该行加载到内存中,然后再次写回。

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? 为什么要重新发明轮子?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM