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.