groue / GRDB.swift

A toolkit for SQLite databases, with a focus on application development
MIT License
6.73k stars 696 forks source link

App Crash When init DatabaseQueue #877

Closed glve1027 closed 3 years ago

glve1027 commented 3 years ago

What did you do?

I just get information from Firebase. I can not reproduce it.

What did you expect to happen?

What happened instead?

Environment

GRDB flavor(s): (GRDB, SQLCipher, Custom SQLite build?) GRDB version: "4.14.0" Installation method: CocoaPods Xcode version: Swift version: 4.2 Platform(s) running GRDB: (iOS, macOS, watchOS?) iOS 12/ iOS 14 macOS version running Xcode:

Demo Project

    private let dbPath: String = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true).last! + "/xxxxxr.db"
    public lazy var dbQueue: DatabaseQueue = {
        let q = try! DatabaseQueue(path: dbPath)
        return q
    }()

Crash Log

Crashed: com.xxxx.excuteDataQueue EXC_BREAKPOINT 0x00000001afea6af0

Fatal error: 'try!' expression unexpectedly raised an error: SQLite error 11 with statement SELECT * FROM sqlite_master LIMIT 1: database disk image is malformed: file /xxx.swift, line 29

groue commented 3 years ago

Hello @glve1027,

"Database disk image is malformed" leaves no doubt on the symptom. But who knows how your app ended up with an invalid database file? You will have to investigate, and grab as much information as possible on how SQLite databases can become corrupt. Starting from SQLITE_CORRUPT looks like a good idea. Please come back when your issue is solved, or if you can tell what GRDB does wrong.

glve1027 commented 3 years ago

@groue Actually. I had check the Stack Trace. Just had Finished this method

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {}

be initing DatabaseQueue property. and app crashed.😿

groue commented 3 years ago

@glve1027, you see an SQLite error when the app opens the database. There's nothing GRDB can do about it. As I said above, you have to look for the original cause of database corruption in your app.

glve1027 commented 3 years ago

@groue okok.

groue commented 3 years ago

okok

Yeah, exactly as you say: "okok". Are you sure the database file is available? Are you sure your app does not manipulate the database with FileManager (generally a bad idea)? Did you click on the above link to SQLITE_CORRUPT, and read (carefully)?

groue commented 3 years ago

I'm closing this issue. Please reopen if you think something has to be done in this repository.

glve1027 commented 2 months ago

After I add some log infos on the app.

Code:

    private static func makeShared() -> XXXDBAppDatabase? {
        do {
            var config = Configuration()
            config.prepareDatabase { database in
                try database.execute(sql: "PRAGMA auto_vacuum = FULL")
            }
            let currentQueue = try DatabaseQueue(path: XXXDBAppDatabase.dbPathV2, configuration: config)

            // Create the AppDatabase
            let appDatabase = try XXXDBAppDatabase(currentQueue)

            return appDatabase
        } catch {
            Log(error.localizedDescription)
            print(error)
            return nil
        }
    }

Error description is:

database disk image is malformed - while executing PRAGMA auto_vacuum = FULL

Above exception happens when above init method.

glve1027 commented 2 months ago

I'm not sure if this has anything to do with the ”auto_vacuum = FULL“.