0

I'm testing on the iOS simulator involving the FMDB distributed along with the MapBox iOS SDK. Here is a bug I can't catch and I wonder if I am breaking some FMDB rules I do not know or I'm having serious memory corruption somewhere (but I've already narrowed the problem down to a few lines). The following code will run into the line that's commented "should not be here". And commenting out any of the lines labeled below would run into "should be here".

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    NSString * sql_gdb = @"/Users/t2wu/Library/Application Support/iPhone Simulator/5.1/Applications/BB193B94-2549-49DB-9BBB-C66D76743515/Library/Application Support/Poki.OfflineSpotty/fremont_spatial.gdb";

    FMDatabase * db = [FMDatabase databaseWithPath:sql_gdb];
    if ([db open]) {
        db.shouldCacheStatements = TRUE; // Correct when this line is commented out

        [self fooDB:db];

        FMResultSet * debug1 = [db executeQuery:@"SELECT id, type, state FROM payloads WHERE id=?", [NSNumber numberWithLong:2]];
        BOOL DOESFIND1 = FALSE;
        while ([debug1 next]) {
            DOESFIND1 = TRUE;
            NSLog(@"Does find."); // Should be here.
        }
        if (DOESFIND1 == FALSE) {
            NSLog(@"Does not find"); // Should not be here.
        }
    }

    return YES;
}

-(void) fooDB: (FMDatabase *)db
{
    NSLog(@"SELECT id, type, state FROM payloads WHERE id=%@", [NSNumber numberWithLong:1]);
    FMResultSet * result = [db executeQuery:@"SELECT id, type, state FROM payloads WHERE id=?", [NSNumber numberWithLong:1]];   // Correct when changed to "SELECT * FROM"  
    while ([result next]) {
        return;  // Correct when this line is commented out.
    }
    return;
}

I've put the code in application:didFinishLaunchingWithOptions just so I can run it as the first thing before any other memory is messed with. (This should come before all views, If I am correct about this.) But I have no idea where the problem is. And again, comment out any of the two lines that says "Correct when" will produce the desired result. Or query with SELECT * instead of SELECT id, type, state will also produce the desired result.

huggie
  • 17,587
  • 27
  • 82
  • 139
  • Perhaps has to do with this issue: https://github.com/ccgus/fmdb/issues/6 – Any reason you can't just turn off statement caching? – omz Jul 24 '12 at 01:46
  • I turn it on for performance reason which I badly needs. Down the page you give, mattstevens says " If each level of nesting uses a unique key things should work as expected." I do use a unique key for both of these queries. – huggie Jul 24 '12 at 02:11
  • Wow I commented out that line and my entire program seems to be working correctly. How did you Google for the bug? Post an answer I'll accept it. – huggie Jul 24 '12 at 03:21
  • Though I'm pretty sad to have found out it feels like its running slower. I might consider using SQL directly. – huggie Jul 24 '12 at 03:24

1 Answers1

0

I would like to comment that the way you have written the Database code and method, fooDB is a bit difficult to read.

Try the following:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 [self fooDB];
 return YES;
}

-(BOOL) fooDB
{

   NSString * sql_gdb = @"/Users/t2wu/Library/Application Support/iPhone Simulator/5.1/Applications/BB193B94-2549-49DB-9BBB-C66D76743515/Library/Application Support/Poki.OfflineSpotty/fremont_spatial.gdb";

   FMDatabase * db = [FMDatabase databaseWithPath:sql_gdb];
   if (![db open]) {
      NSLog(@"Could not open DB.");
      return NO;
   }    

   FMResultSet * result = [db executeQuery:@"SELECT id, type, state FROM payloads WHERE id = ?", [NSNumber numberWithLong:1]];   
  while ([result next]) {
      //DO SOMETHING HERE
  }
  [result close];
  [db close];
  return YES;
}
Ravi Raman
  • 1,070
  • 9
  • 16