marcoarment / FCModel

An alternative to Core Data for people who like having direct SQL access.
MIT License
1.65k stars 173 forks source link

FCModelDatabaseQueue has an open FMResultSet after inDatabase #147

Closed Codeglee closed 6 years ago

Codeglee commented 7 years ago

Since upgrading from Xcode 6 to 8 and updating to Swift 3.1, on the second and every future run of the app I get: FCModelDatabaseQueue has an open FMResultSet after inDatabase:

On app start didFinishLaunchingWithOptions I go through a migration process consisting of:

let dbPath = DbMigration.DbPath() //Which would be ... app support folder/MyApp.sqlite3
FCModel.openDatabase(atPath: dbPath) { (fmDb:FMDatabase?, version:UnsafeMutablePointer<Int32>?) in
    if let db = fmDb {
        db.traceExecution = false
        db.crashOnErrors = true
        db.beginTransaction()

        if let schemaVersion = version {
            let swiftSchemaVersion = Int32(schemaVersion.pointee)
            let updatedSchemaVersion : Int32 = swiftSchemaVersion + 1
            let success = DbMigration.executeEntityScriptsAgainstDatabaseForVersion(db, currentSchemaVersion:Int(swiftSchemaVersion));
            if(success.hadMigrations && success.migrationsSucceeded) {
                schemaVersion.pointee = updatedSchemaVersion
            }
            else if (success.hadMigrations && !success.migrationsSucceeded){
                db.rollback()
            }
        }
        db.commit();
    }
}

DbMigration.executeEntityScriptsAgainstDatabaseForVersion just contains table insert and update scripts such as:

var scriptLines = [String]()
if(version < 1) {
    let initialCreation = [
        "CREATE TABLE IF NOT EXISTS Project (\n",
        "projectId INTEGER PRIMARY KEY NOT NULL,\n",
        "title TEXT DEFAULT '',\n",
        "summary TEXT DEFAULT '',\n",
        "startDate REAL NOT NULL,\n",
        "endDate REAL NOT NULL,\n",
        "targetWordCount INTEGER NOT NULL DEFAULT 0,\n",
        "targetDailyWordCount REAL NOT NULL DEFAULT 0,\n",
        "selectedSceneId INTEGER NOT NULL DEFAULT 0\n",
        ");"
    ]
    scriptLines.append(contentsOf: initialCreation)
}

@jasonsilberman previously encountered and fixed this in his issue https://github.com/marcoarment/FCModel/issues/67 but I haven't been able to find out how.

Anyone who's seen this issue and knows what causes it, it's stopping me from upgrading my app, effectively leaving it dead in the water, please let me know.

Codeglee commented 6 years ago

For completeness, I still don't know what was causing it but before exiting openDatabase I just added:

 if db.hasOpenResultSets() {
     db.closeOpenResultSets()
}