简体   繁体   中英

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. 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. 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? 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?

How can I know when to save the data inside the application if I'm using an NSMutableArray of dictionaries?

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 .

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. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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