简体   繁体   English

保存数据以便在运行时使用

[英]Saving data for use at runtime

I've got an iOS app I am trying to build that presents the user data from a .xlsx (MS Excel file) I have. 我有一个我正在尝试构建的iOS应用程序,它提供了我的.xlsx(MS Excel文件)中的用户数据。 The file has nearly 11,000 rows, with 20 columns per row. 该文件有近11,000行,每行20列。 I have built a parser to transform each row into an subclass of NSObject , with each column being an instance variable. 我已经构建了一个解析器来将每一行转换为NSObject的子类,每列都是一个实例变量。 I made it NSCoding compliant, added each instance to an NSDictionary , with it's key being the first row of the original file, and archived it. 我使它符合NSCoding ,将每个实例添加到NSDictionary ,其密钥是原始文件的第一行,并将其存档。 My problem is, I don't want to unarchive such a massive NSDictionary , to access the maximum of 20 or objects with in it, I might need at any given time. 我的问题是,我不想取消归档如此庞大的NSDictionary ,最多可以访问20个或其中的对象,我可能需要在任何给定的时间。

The question is: How do I go about saving this chunk of data in such a way, I don't have 11,000 objects living in the heap, and still be able to load the ones I need into the app while it's running? 问题是:如何以这种方式保存这些数据块,我没有11,000个对象存在于堆中,并且仍然能够在应用程序运行时将我需要的对象加载到应用程序中?

I believe your best option is to export your data to a database. 我相信您最好的选择是将数据导出到数据库。

The simplest solutions are SQLite or CoreData . 最简单的解决方案是SQLiteCoreData

I would go with CoreData as it will save you a lot of development time (you will need to spend some time to get acquainted with CoreData though). 我会选择CoreData,因为它可以节省大量的开发时间(你需要花一些时间来熟悉CoreData)。 In addition, it will probably take care of your memory issues regarding holding only the objects you use in memory. 此外,它可能会处理有关仅保留您在内存中使用的对象的内存问题。

Basically, CoreData is an object graph, which mean that it lets you load portions of the graph only when needed (for SQLite store type). 基本上,CoreData是一个对象图,这意味着它允许您仅在需要时加载图形的一部分(对于SQLite商店类型)。

From what you described, you have a single entity (row with 20 columns) that you would like to access using some kind of query without loading all the entities in the file. 根据您的描述,您可以使用某种查询访问单个实体(包含20列的行),而无需加载文件中的所有实体。

The CoreData equivalent would be: CoreData等价物将是:
1)Define the entity in core data (once, in the model designer) 1)在核心数据中定义实体(一次,在模型设计器中)
2)Create the CoreData stack (for a simple design this is automatically generated) 2)创建CoreData堆栈(对于简单设计,这是自动生成的)
3)Create a NSPredicate to select the instances you like 3)创建NSPredicate以选择您喜欢的实例
4)Create a NSFetchRequest to define the access to the data 4)创建NSFetchRequest以定义对数据的访问
5)Execute the request on the main context using a NSFetchedReaultsController 6)Display your results 5)使用NSFetchedReaultsController在主上下文上执行请求6)显示结果

You could create a new project that uses core data and see an example of how this works 您可以创建一个使用核心数据的新项目,并查看其工作原理的示例

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

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