views:

259

answers:

1

Hey Folks,

I'm developing for iphone-sdk 2.2.1 (so no CoreData cry).

So I'm using the FMDatabase project, which is just a SQLite Wrapper in Obj C. I have my DB working well when interacting with it from my AppDelegate. I've tested connecting, INSERTING, etc from the AppDelegate.

Now, I have a data object that I want to store into the Database. I would like this event to occur from a ViewController class. Lets see some code:

App Delegate Creates the DB in the applicationDidFinishLaunching method. The DB is declared as a property of this class so I can easily access it.

db = [FMDatabase databaseWithPath:[self getDBPath]];

This works fine, I have tested it. The only thing I'm a little concerned about is 'db' being a property of the class. That shouldn't cause problems, should it?

From the same applicationDidFinishLaunching method, I've tested that a simple insert works using. This Works:

[db beginTransaction];
[db executeUpdate:@"INSERT INTO tblDataSamples (...) VALUES (...);"];
[db commit];

Now, if I simply take this exact code and move it to an instance method of the AppDelegate, the code no longer works. I get a "EXC_BAD_ACCESS" error when we hit the [db beginTransaction] line.

The flow of this code to this call: - AltViewController Receives a button tap Event - AltViewController tells ApplicationDelegate to execute the "addSample" method. - ApplicationDelegate's addSample method fails on [db beginTransaction].

Now that I've written this out, I am thinking the problem is that my DB gets lost after the ApplicationDelegate loads the RootView. Maybe I'm wrong though. Anyone have any ideas?

!!!!!!!!!

UPDATE! I just altered my addSample method to include

db = [FMDatabase databaseWithPath:[self getDBPath]];
    if (![db open]) {
        NSLog(@"Could not open db.");
    }

and

[db close];

The entire transaction works now. So, the new question is: how do I make the database open ONCE and stay open across different views and viewControllers?

+2  A: 

You're having trouble because you don't know the memory management rules yet. In this case, the databaseWithPath: method returns an autoreleased object, which is disposed of at the end of the run loop. Your db pointer still points to that invalid memory location though, which is why you're seeing the EXC_BAD_ACCESS error when you try to access it later on.

The good news is that memory management is actually pretty easy to learn in Cocoa. Have a look at this page for starters, it should have enough information to get you going. To solve this particular problem you would need to call the retain method on your db object after it's created, and release it when it's no longer needed (if you're keeping it for the life of your application there's nothing gained by releasing it, but it's still good practice).

Marc Charbonneau
So, what is the better way to interact with the DB? Open and close it each time you want to perform a transaction or keep it open for the lifetime of the application? Right now, I'm opening and closing each time because I didn't +1 to db's retain count. Would it be better to retain this at creation in my AppDelegate? Also, would it be beneficial to declare the DB as static?
Buffalo
I would start by putting the db instance in one of your global, application lifetime controllers (like the app controller), but the final design should depend on your own design and performance considerations.
Marc Charbonneau