FMDatabase wrapper, initialising issue??

Discussion in 'iOS Programming' started by mraheel, Apr 16, 2010.

  1. mraheel macrumors regular

    Joined:
    Apr 18, 2009
    #1
    Hi guys,

    I've run into a simple complication, I dont think i got how FMDatabase can be initialised and used throughout the application in similar ways of Sqlite.

    For example,
    Code:
    	NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
    
    FMDatabase *newDB = [FMDatabase databaseWithPath:path];
    
    if (![newDB open]) {
    	}
    FMResultSet *rs = [newDB executeQuery:@"select * from tap where id=?",  @"2"];
    		if ([newDB hadError]) {
    			NSLog(@"Err %d: %@", [newDB lastErrorCode], [newDB lastErrorMessage]);
    		}
    		
    		while ([rs next]) {
                        NSLog(@"%@", [rs stringForColumn:@"Name"]);
    		}
    		[rs close];
    	
    	[newDB close];
    The above code works perfectly fine, when called from anywhere..

    However, If I actually Synthesize the FMDatabase in appDidFinishLaunching, and utilise that database for various other uses, it fails.

    in AppDelegate.h
    Code:
     FMDatabase *DB;
    
    @property(nonatomic, retain) FMDatabase *DB;
    in AppDelegate.m
    Code:
    @synthesize DB;
    -(void)applicationDidFinishLaunching{
    	NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
       DB = [FMDatabase databaseWithPath:path];
       if (![DB open]) {
        NSLog(@"cannot open db");
       }
    
    }
    
    -(void)doSomethingWithDb{
          FMResultSet *rs = [DB executeQuery:@"select * from tap where id=?",  @"2"];
    		if ([newDB hadError]) {
    			NSLog(@"Err %d: %@", [DB lastErrorCode], [DB lastErrorMessage]);
    		}
    		
    		while ([rs next]) {
                        NSLog(@"%@", [rs stringForColumn:@"Name"]);
    		}
    		[rs close];
    }
    
    I thought atleast, theoretically this would work, but it didnt.
    When i called -doSomethingWithDb at runtime, i got the error:

    objc_msgSend

    and this is the line it highlights,
    Code:
    FMResultSet *rs = [theFileb executeQuery:@"select * from tap where id=?",  @"2"];
    theres no mention of what else is wrong, in console.


    Whats going wrong here? There arent too many examples of how FMDb is to be created initialised. As I plan to call it from many other viewControllers.

    Any suggestion is appreciated.

    But the code at the top, where a whole new FMDatabase is initialised everytime during the app works perfectly, but theres a certain lag which is either cuz of the FM wrapper itself, i read somewhere about that, or its because of the database being initialised and opened at every call.
     
  2. bredell macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #2
    You've forgotten to retain the database. The FMDatabase:databaseWithPath: method returns a database object that's autoreleased. That's why it works when you're using the database immediately after opening it, but not at a later time.
     
  3. mraheel thread starter macrumors regular

    Joined:
    Apr 18, 2009
    #3
    Oh man, I dunno how I missed that!..

    thanks man,

    speaking of FMDB, if anyone used it, Does it have better performance? specially when using the Cache feature, I havent noticed much.
     

Share This Page