简体   繁体   English

为什么我不应该在iOS上将数据存储为文件?

[英]Why should I not store data as files on iOS?

If I have a collection of around 100 entities that I need to store, each with a few KB of data, what problems could I encounter if I were to choose to save and read them as files, instead of using Core Data or sqlite or any other database? 如果我有大约100个需要存储的实体的集合,每个实体都有几KB的数据,如果我选择选择将它们保存并读取为文件,而不是使用Core Data或sqlite或任何其他实体,会遇到什么问题?其他数据库?

I'll most likely need to use only a few at a time, and I plan on reading, parsing and loading them in memory, using them, then releasing them when they are of no more use. 我很可能一次只需要使用几个,并且我计划读取,解析并将它们加载到内存中,使用它们,然后在不再使用它们时释放它们。 Also, I'll never need to search for data inside them and I'll always access them by their ID. 另外,我永远不需要在其中搜索数据,而且我将始终通过其ID访问它们。

What database offers you is representing relationship between pieces of data, integrity maintenance, transactional support, and indexing for fast searching and access. 数据库为您提供的内容代表着数据之间的关系,完整性维护,事务支持以及用于快速搜索和访问的索引。 (Well, all that and a bit more...) (好吧,还有更多……)

On the flip side, databases are notoriously inefficient when you have to deal with non-structured or non-uniform data - binary blobs like images, XML files each with different loosely defined schema, and so on. 另一方面,众所周知,当您必须处理非结构化或非均匀数据时,数据库效率低下-二进制Blob(例如图像),每个XML文件都具有不同的松散定义的架构,等等。 Hence why nobody uses relational databases for caching purposes, for example. 因此,例如为什么没有人将关系数据库用于缓存目的。

So, whether you should use Core Data (or another database) or files depends really on the content of these files, what their relationship is, and what you need to do with them. 因此,您是否应该使用Core Data(或其他数据库)或文件,实际上取决于这些文件的内容,它们之间的关系以及与它们的关系。

It's perfectly fine to embed lot's of files - I suspect the file system to be pretty efficient. 嵌入很多文件是完全可以的-我怀疑文件系统效率很高。 If you need to access or filter them on some attributes, or read data partially, or do other fancy things with them, this is where a database comes in handy. 如果您需要在某些属性上访问或过滤它们,或部分读取数据,或对它们进行其他处理,这是在数据库中很方便的地方。

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

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