iOS: Accessing to sqlite database using singleton or not

I have an app with a simple table stored in a common sqlite database. The app has a mainview and several other views say, view1, view2, ....,viewN. From a mainview, the user go to view1 by this code section:

        [self presentModalViewController:screen animated:YES];

in view1, the user will access the database, doing something, then update the database, quite view1 back to the mainview:

[self dismissModalViewControllerAnimated:YES];

The user will do the same thing for the other views, i.e., accessing the database, doing something, updating the database, then back to the mainview.

My question is how should I organize the database in my case, using a singleton to create a common object to open the database at the mainview, then all views will access the database, updating it or each view will open the database, accessing it, then update individually or is there any other efficient way. Thank you


As you describe the structure of your app being one threaded only - using a singleton is perfectly OK. You need to open the DB only once when the app starts and make sure to close it when the app ends or even when the app goes to background (of course then you need to open the DB when return from background too)

By the way, I also tried to open and close the DB for each view - which also works fine. In this approach I also sometimes use a "dirty" flag, that is set to indicate that the DB needs updateing before closing - but that turned out to make no difference in performance.

Instead of using a singleton you may also use a class variable or declare it within your app delegate, which is often done for the cotext of core data ( where the "context" in core data is similar to the DB in your case)

What is important in whatever approach you use is that your DB will be in a consistent state all the time since your app can get "interrupted" by phone call for example.

By the way, I tend to use core data more often on iPhone if circumstances allow, since core data takes care of many of the DB issues - only saving at consistent states needs to be done explicitly. But it really depends if your data is more a big DB or just "some" persitent objects/attributes.

Need Your Help

Why is my if statement not working the way I expect?

php mysql if-statement

I am trying to achieve the following: I ask my SQL database a query using SELECT * FROM subjects. After doing that I ask for the array using mysqli_fetch_assoc. Until that point all is fine. The pr...

Editing existing entries in Django forms

django forms django-forms django-views

My problem is similar to how to edit model data using django forms, but I'm not able to solve it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.