GRDB.swift: Crash when using .filter(SQLExpressible) within a Record class

What did you do?

We were upgrading GRDB from version 2.4.0 to 2.8.0 throughout our apps

What did you expect to happen?

The apps should work as usual, no changes to our code

What happened instead?

The apps started crashing when doing some Record queries.

Environment

GRDB flavor(s): GRDB GRDB version: 2.8.0 (vs. 2.4.0) Installation method: manual (pre-built framework) Xcode version: 9.2 Swift version: 4.0 Platform(s) running GRDB: iOS macOS version running Xcode: 10.12.6

Demo Project

Playground

import GRDB

// Use a memory database for quick prototyping, with minimal table
let dbQueue = DatabaseQueue()
try! dbQueue.inDatabase { db in
    try db.execute("""
        CREATE TABLE objecta(a,b);
        INSERT INTO objecta(a,b) VALUES ("TestA1","TestB1");
        INSERT INTO objecta(a,b) VALUES ("TestA2","TestB2");
        INSERT INTO objecta(a,b) VALUES ("TestA3","TestB3");
        INSERT INTO objecta(a,b) VALUES ("TestA4","TestB4");
        """)
}

class ObjectA: Record {
    override class var databaseTableName: String { return "objecta" }
    
    class func count(query: SQLExpressible? = nil) -> Int {
        if query == nil {
            return dbQueue.inDatabase { db in
                try! self.fetchCount(db)
            }
        } else {
            return dbQueue.inDatabase { db in
                try! self.filter(query!).fetchCount(db)
            }
        }
    }
    
    static func findAll(query aQuery: SQLExpressible? = nil) -> [ObjectA] {
        let result: [ObjectA]
        
        if aQuery == nil {
            result = dbQueue.inDatabase { db in
                try! self.fetchAll(db)
            }
        } else {
            result = dbQueue.inDatabase { db in
                try! self.filter(aQuery!).fetchAll(db)
            }
        }
        
        return result
    }
}

// This works
print(ObjectA.count())
print(ObjectA.findAll().count)

// This works in GRDB 2.4.0, crashes in 2.8.0
print(ObjectA.count(query: Column("a") == "TestA1"))
print(ObjectA.findAll(query: Column("a") == "TestA1").count)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 23 (10 by maintainers)

Most upvoted comments

OK, I may have found the point: Framework Search Paths.

Linking & embedding the right framework isn’t enough, for some reason Xcode was still looking at another binary if it was somehow within its reach. As my project embedded multiple versions of the frameworks for demo purposes, the problem was showing itself…

Sorry for bothering you, learned something for sure!