stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.59k stars 1.55k forks source link

getting exception on iterator #1036

Closed avnerbarr closed 2 years ago

avnerbarr commented 3 years ago

getting a crash here when the DB is open in 2 processes:

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: database is locked (code: 5)
extension FailableIterator {
    public func next() -> Element? {
        return try! failableNext()
    }
}

// when iterating
for row in try db.prepare(...) {
        }
juanjoseluisgarcia commented 3 years ago

SQLite blocks the file when you access it. You need to keep a global reference to the connection to avoid this kind of issues. On another matter of things, this repo is abandoned. The Author is involved in some kind of online teaching courses and even though he has activity on other repos he seems to not have the courage to announce he will not be continuing with this one. My advice is that you switch to another library like GRDB.

nathanfallet commented 2 years ago

We merged a fix replacing the try! by a try?.

tw0po1nt commented 1 year ago

@NathanFallet I am still getting this crash on the latest release 0.14.1. It appears the try? you mention is still a try!, so this is still an issue.

nathanfallet commented 1 year ago

@tw0po1nt We reverted this fix; as explained in #1075, you should use failableNext() and handle the error by yourself using a do/catch block. We cannot provide a default behavior (e.g. return nil) when an error occurs, it's up to you to choose what you want to do.