简体   繁体   English

在应用程序内部使用SQLite数据库的最佳方法

[英]Best way to work with SQLite database inside application

Sorry if it has been asked before, I can't find any question about this issue and I'm new to Obj-C so please be patient with me. 抱歉,如果之前曾有人问过,我找不到关于此问题的任何问题,而且我是Obj-C的新手,所以请耐心等待我。 Thanks in advance. 提前致谢。

I have an application that works with SQLite3 database, I want to work with the database inside the application, In every UIViewController I'm loading the data again from the database. 我有一个适用于SQLite3数据库的应用程序,我想与该应用程序内部的数据库一起使用,在每个UIViewController我都从数据库中再次加载数据。 The result is that the application is working a little bit slow in some views and more in others. 结果是该应用程序在某些视图中的工作速度有点慢,而在其他视图中的工作速度则有些慢。

So, my question is, how to work with an SQLite3 database? 所以,我的问题是,如何使用SQLite3数据库? Is the best way is to load the SQLite3 database inside an NSMutableArray of NSMutableDictionary's or to work from the app it self, each time to call the database and to save inside of it? 最好的方法是在每次调用数据库并将其保存在NSMutableArrayNSMutableDictionary's内的NSMutableArray中加载SQLite3数据库,或从其自身的应用程序进行操作吗?

How can I know when to save the data inside the application if I'm using an NSMutableArray of dictionaries? 如果我使用字典的NSMutableArray ,如何知道何时在应用程序中保存数据?

Thank you very much. 非常感谢你。

It depends a lot upon the nature of your data, but when I'm using a database, it's generally because I have enough data that I would rather not use up precious memory, and therefore I tend to retrieve data from the database as I need it, view controller to view controller. 这很大程度上取决于您的数据的性质,但是当我使用数据库时,通常是因为我有足够的数据,我不想用完宝贵的内存,因此我倾向于根据需要从数据库中检索数据它,查看控制器以查看控制器。 Memory is one of the more scarce resources on iOS devices, so be careful before you decide to blithely hold data in a NSArray / NSDictionary . 内存是iOS设备上较为稀缺的资源之一,因此在决定将数据巧妙地保存在NSArray / NSDictionary之前,请务必小心。

Obviously, it's better if you don't have your SQL code littered throughout your app, so you probably want to encapsulate all of the data retrieval within a single class, if possible. 显然,最好不要在整个应用程序中乱扔SQL代码,因此,如果可能的话,您可能希望将所有数据检索封装在一个类中。 That way you can change the data storage/retrieval mechanisms at some later date, as needed, and you don't have to change the code all over your program. 这样,您可以在以后根据需要更改数据存储/检索机制,而不必在整个程序中都更改代码。

In terms of your observation that the app is a little slow retrieving data from the database, that's very strange, because retrieving data from a database is generally super quick. 根据您的观察,该应用程序从数据库中检索数据有点慢,这很奇怪,因为从数据库中检索数据通常超级快。 The only time I've had performance issues is when I was storing BLOB objects (eg for the PNG/JPG representations of images) in the database. 我遇到性能问题的唯一时间是在数据库中存储BLOB对象(例如,图像的PNG / JPG表示形式)时。 So, now, unless I'm dealing with tiny, thumbnail-sized images, I'll store the images in the Documents folder and the database will only maintain the path for those image files. 因此,现在,除非我要处理微小的缩略图图像,否则将图像存储在Documents文件夹中,数据库将仅维护这些图像文件的路径。

But, getting back to your question, if your app is slow, you might want to submit a different question outlining the precise nature of the data and outline the nature of the performance hits you're suffering, and I'm sure we can try to help you out. 但是,回到您的问题上,如果您的应用程序运行缓慢,则可能要提交一个不同的问题,概述数据的确切性质并概述所遭受的性能影响的性质,我敢肯定,我们可以尝试帮你

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

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