简体   繁体   English

在iOS应用程序中存储静态数据的最佳方法是什么?

[英]What's the best way to store static data in an iOS app?

I have in my app a considerable amount of data that it needs to access, but will never be changed by the app. 我在我的应用程序中有大量需要访问的数据,但应用程序永远不会更改。 Currently I'm using this data in other applications in JSON files and SQL databases, but neither seems very straightforward to use in iOS. 目前我在JSON文件和SQL数据库中的其他应用程序中使用此数据,但在iOS中使用它们似乎都不是很简单。

I don't want to use CoreData, which provides tons of unnecessary functionality and complexity. 我不想使用CoreData,它提供了大量不必要的功能和复杂性。

Would it be a good idea store the data in PropertyList file and build an accessor class? 将数据存储在PropertyList文件中并构建一个存取器类是一个好主意吗? Are there any simple ways to incorporate SQLite without going the CoreData route? 是否有任何简单的方法可以在不使用CoreData路由的情况下合并SQLite?

You can only use plist if the amount of data is relatively small. 如果数据量相对较小,则只能使用plist。 Plist are entirely loaded into memory so you can only really use them if you can sustain all the objects created by the plist in memory at once for as long as you need them. Plist完全加载到内存中,因此只有在您需要的时候,如果您可以在内存中同时维护plist创建的所有对象,您才能真正使用它们。

Core Data has a learning curve but in use it is usually less complex than SQL. 核心数据具有学习曲线,但在使用中它通常不如SQL复杂。 In most cases the "simpler" SQL leads to more coding because you end up having to duplicate much of the functionality of Core Data to shoehorn the procedural SQL into the object-oriented API. 在大多数情况下,“更简单”的SQL会导致更多编码,因为您最终必须复制Core Data的大部分功能,以便将过程SQL强加到面向对象的API中。 You have to manually manage the memory use of all the data by tracking retention. 您必须通过跟踪保留来手动管理所有数据的内存使用。 You've write a lot of SQL code every time you want data. 每次需要数据时都会编写很多SQL代码。 I've updated several apps from SQL to Core Data and in all cases the Core Data implementation was smaller and cleaner than the SQL. 我已经将SQL中的几个应用程序更新为Core Data,并且在所有情况下,Core Data实现都比SQL更小,更清晰。

Neither is the memory or processor "overhead" any larger. 内存或处理器的“开销”都不大。 Core Data is highly optimized. 核心数据经过高度优化。 In most cases, off the shelf Core Data is more efficient than hand tuned SQL. 在大多数情况下,现成的Core Data比手动调整的SQL更有效。 One minor sub optimization in SQL usually destroys any theoretical advantage it might have. SQL中的一个次要子优化通常会破坏它可能具有的任何理论优势。

Of course, if you're already highly skilled at managing SQL in C then you personally might get the app to market more quickly by using SQL. 当然,如果您已经非常熟练地使用C语言管理SQL,那么您个人可能会通过使用SQL来更快地将应用程序推向市场。 However, if you're wondering what you should plan to use in general on on Apple Platforms, Core Data is almost always the answer and you should take the time to learn it. 但是,如果你想知道你应该计划在Apple平台上使用什么,核心数据几乎总是答案,你应该花时间去学习它。

You can just use SQLite directly without the overhead of Core Data using the SQLite C API . 您可以直接使用SQLite,而无需使用SQLite C API的Core Data开销。

Here is a tutorial I found on your use-case - simply loading some data from an SQLite database. 是我在您的用例中找到的教程 - 只需从SQLite数据库加载一些数据。 Hope this helps. 希望这可以帮助。

Depending on the type of your data, the size and how often it changes, you may desire to just keep things simple and use a property list. 根据数据类型,大小和更改频率,您可能希望保持简单并使用属性列表。 Otherwise, using SQLite (documented in Jergason's answer) would be where I'd go. 否则,使用SQLite(在Jergason的答案中记录)将是我要去的地方。 Though let me say that if you have a relatively small (less than a couple hundred) set of basic types (arrays, dictionaries, numbers, strings) that don't change frequently, then a property list will be a better choice in my opinion. 虽然让我说如果你有一个相对较小(不到几百)的基本类型(数组,字典,数字,字符串)不经常更改,那么在我看来属性列表将是一个更好的选择。

As an example to that, in one of my games, I create the levels from a single property list per difficulty. 作为一个例子,在我的一个游戏中,我根据每个难度从单个属性列表创建级别。 Since there are only a handful of levels per difficulty (99) and a small set of parameters for each (number of elements in play, their initial positions, mass, etc) then it makes sense, and I avoid having to deal with SQLite directly or worse yet, setting up and maintaining CoreData. 由于每个难度只有少数几个级别(99)和每个级别的一小组参数(游戏中的元素数量,它们的初始位置,质量等)然后它是有道理的,我避免直接​​处理SQLite或者更糟糕的是,设置和维护CoreData。

What do you mean by "best"? “最好的”是什么意思? What kind of data? 什么样的数据?

If it's a bunch of objects, then JSON or (binary) plist aren't terrible formats, since you'll want the whole thing loaded in memory to walk the object graph. 如果它是一堆对象,那么JSON或(二进制)plist并不是可怕的格式,因为你需要在内存中加载整个东西来遍历对象图。 Compare space efficiency and loading performance to pick which one to use. 比较空间效率和装载性能以选择使用哪一个。

If it's a bunch of binary blobs, then store the blobs in a big file, memory-map the file (NSDataReadingMapped aka NSMappedRead), and use indexes into the blobs. 如果它是一堆二进制blob,则将blob存储在一个大文件中,内存映射文件(NSDataReadingMapped又名NSMappedRead),并将索引用于blob。 iOS frameworks use a mixture of these (eg there are a lot of .pngs, but also "other.artwork" which just contains raw image data). iOS框架使用这些的混合(例如,有很多.png,但也包含“other.artwork”,它只包含原始图像数据)。

You can also use NSKeyedArchiver and friends if your classes implement the NSCoding protocol, but there's some object graph management overhead and the plist format it produces isn't exactly nice to work with. 如果你的类实现了NSCoding协议,你也可以使用NSKeyedArchiver和朋友,但是有一些对象图管理开销,它产生的plist格式并不是很好用。

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

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