0
votes

In my current app I have nearly 10 scenarios of inserting, updating , deleting and selecting the data into and from the sqlite3 database. Now for every query Iam opening the database and closing the database this makes my app very slow.
I have tried other way like opened the database at the launch of app (applicationDidBecomeActive:)and closed the database when app terminates(applicationWillResignActive/ applicationWillTerminate:) only once.
This is working only when we install the app first time.Once i deleted the current build and relaunching the app is not working. The error is Database is locked. Please help me. which way is the better one.

4

4 Answers

0
votes

Instead of opening your database every time

What you can do

1) Initialize an instance of NSMutableArray in your AppDelegate Class and set its property strong and nonatomic.

for example @property(strong,nonatomic)NSMutableArray *temp;

2) Take a backup of your database in temp i.e NSMutableArray object

3) Perform an operation inserting, updating , deleting and selecting the data into temp i.e. NSMutableArray object

4) In background, perform same operation in your database. So, this will block your UI section.

Through this process, you are performing all your operation in an array not directly to your database. This will help not to open your database every time and will make your application little bit faster.

0
votes

Are you sure your database was closed correctly ?

If it was not closed correctly, your database can become corrupted. The file should not be locked by the OS anymore past the end of the application (if this application is not configured to run in background of course).

When you close your database, are you sure that all statements are finished and finalized ?

-1
votes

try opening your database in

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

this methods everytime your application is launched

-1
votes

This is not a good idea to open an close the db-file when application become action and when goes to background then close it, always open db and fetch data and close connection. Try to implement the singleton pattern for your database connection class or use sqlite wrapper like FMDB!