Can you please give some suggestions on sqlite using on the iPhone?
Within my application, I use a sqlite DB to store all local data. Two methods can be used to retrieve those data during running time.
1, Load all the data into memory at initialization stage. (More memory used, less DB open/close operation needed)
2, Read corresponding records when necessary, free the occupied memory after using. (Good habit for memory using, but much DB open/close operations needs).
I prefer to use method 2, but not sure whether too many DB opening/closing operations could affect app's efficiency. Or do you think I can 'upgrade' method 2 by opening DB when app launches and closing DB when app quits?
Thanks for your suggestions very much!
First of all: use FMDB to access SQLite!
Then, create your own singleton "MyDB" class.
Every time you need the database, you do [MyDB instance] to get your FMDB instance.
That way you only have one DB open (in didFinishLaunching) an you close it when your app exits.
That's far and away the best way to use SQLite on the iPhone.
The other option is to use CoreData, something I find great for when you start with an empty database, but FMDB/SQLite works best for me if I have a set of data that's read-only.
Apple seems to suggest that you avoid preloading all the data during the startup in order to ensure faster and smoother startup. Supposedly you should only load data when/if the user needs it.
Related
I need to save my objects in permanent memory. The option I use right now is that i save my objects in the NSUserDefaults before my app quits and I retrieve them when my app starts running. This approach is not very convenient since I may lose important data in case the application crashes. Is there any way to store my objects, but when a property of an object changes, then this change is saved in the disk automatically? Except for that, there is a danger to mess the objects in ram and the objects in the disk using that architecture.
For example
-> Load objects from memory
-> [object1 setValue:#"5"]
-> Application crashes
After the crash, when the user opens the application, the value #"5" will not be available because I never saved the data.
Is there any alternative so as to make by code more safe and maintainable? CoreData is a good option for this problem , or is it an overhead?
If you need to save data, CoreData is the best. You can save after every change if you need to.
If saving info is so important you might want to figure out what could cause/is causing your application to crash and fix that. No matter how good a solution is if your app is going to randomly crash you'll probably lose some data.
It is really not the best of option to save data instantly when a value changes. It's way too expensive for app performance. You must continue using NSUserDefaults to store the values when app is force closed or entered in background.
Your app is designed by you. It MUST not crash but even if it does, don't worry. Anamolies cannot really be handled.
Hello I'm new to this forum (signed up today... have always used it for reference in the past though) and new to iOS development (6 months), but not a new programmer. I'm creating an app in iOS 5 for iPad that will require the user to rapidly input and update data (mainly adding, subtracting , changing BOOL states, etc... with time between events less than 2 seconds at times) for numerous objects. It is currently designed to use a SQL database, but worry that with rapid entries and updating that the database will get corrupt if it is not opened and closed rapidly enough. Any suggestions or lessons learned from iOS/iPad/iPhone experience? Is SQLite a preferred method for rapid input or should I switch to something else? Thanks and taker easy!
Core data handles all of the database connections for you. Changing properties on your managed objects doesn't involve a connection to the database every time, it just updates the objects in memory. The database is updated when you save the context and probably periodically in the background - the point is, you don't have to worry about it.
Don't code around a problem that doesn't exist - write your app using core data, and if you get database corruption problems, investigate then. But I doubt you will have anything to worry about on that score.
I have an app that must get data from the Sqlite database in order to display the first element to the User.
I have created a domain object which wraps the DB access and is a thread safe singleton.
Is this following strategy optimal to ensure the fastest load given the iPhone's file access and memory management capabilities in threaded apps:
1) In the AppDelegate's FinishedLaunching event the very first thing I do is create the domain singleton within a new thread. This will cause the domain object to go to Sqlite and get the data it needs without locking the UI thread.
2) I then call the standard Window methods to add the View and MakeKeyAndVisible etc.
Is there an earlier stage in the AppDelegate where I should fire off the thread that creates the Domain Object and accesses Sqlite?
Heh, you can go all the way back to the app's execution entry point and create your own thread before invoking UIApplicationMain... that's overkill.
applicationDidFinishLaunching is the best place to do it, if you're worried about fast loading a better approach would be to cache the data in your plist or NSUserDefaults and then update it a couple hundred millisecs later when the DB is ready.
I'm new to working with iphone and SQLite i have my app working fine with SQLite but i would like to know what you all think is the correct / best way of opening an sqlite connection and closing it..
Would you
Initialise a DB connection on app load and close on app close..
Open and close connections when is needed..
Also is it good practise to constantly update db entries as you go or store all information in an object and write out at the end when needed..
There is not to much information on this via google just some useful small tutorials that cover delete / insert / update but not as a general overview of the best practise in using SQLite with iphone..
Thanks guys
Keep the connection open.
Whenever you execute a SQL query, it has to be compiled into a prepared statement. Once it's compiled, you can use it over and over and it doesn't have to be compiled again.
However, the query is compiled against the database pointer, so if you close your connection, you have to release all of your prepared statements. When you re-open your connection, you then have to re-prepare each statement as you use it again.
Save an object whenever you make a set of changes. If you are changing several things on an object (name, address, etc.) make all the changes first, then save it. Don't wait to commit several unrelated changes all at once, though (i.e. at the termination of the app). Related changes can be delayed until they are all made, before saving them.
I am writing an iPhone app that uses SQLite. I am use to opening and closing my connections every time I use a database. However, I do not know if that is a good practice in the iPhone/SQLite environment. I want to know if I should open the database 1 time or if it is OK to open and close the database each time I use it. Please let me know.
I believe you should keep it open as long as you can, so data is cached in DRAM. Of course, you should also organize your transactions so you commit at logical points in time and maintain transactional integrity.
I would do as Matthew suggested: keep one connection opened for as long as your program is running.
Both answers seem right, but actually it depends from how often you're using it and how large is it. In case DB is large you should set larger page cache, but that leads to larger memory consumption and if access is rare - no reason for holding it up all the time (but if usage also small - large page cache won't help you also).
In case it's small - there is no reason to open/close it each time even with infrequent usage. But in average your resource consumption is higher with regular open/close. So all in all - don't reopen db each time you're using it.