mercredi 22 avril 2015

Why are my sqlite calls on separate threads at the same time?

I have a class at http://ift.tt/1y4SKHy that I am trying to make sure is thread safe. Sqlite requires that calls be made by only 1 thread at a time.

I have a queue called _dbQueue

private let _dbQueue = dispatch_queue_create("com.AaronLBratcher.ALBNoSQLDBQueue", nil)

and I open the db synchronously on this queue

    ...
    var openDBSuccessful = true

    //create task closure
    let openFile:() = {
        openDBSuccessful = self.openDBFile(dbFilePath)
        }()

    dispatch_sync(_dbQueue) {
        openFile
    }
    ...

I also run commands and queries synchronously on the same queue

private func sqlExecute(sql:String)->Bool {
    var successful = true

    //create task closure
    let command:() = {
        successful = self.runCommand(sql)
        }()

    dispatch_sync(_dbQueue) {
        command
    }

    return successful
}

func sqlSelect(sql:String)->[DBRow]? { // DBRow is an array of AnyObject
    var recordset:[DBRow]?

    let query:() = {
        recordset = self.runSelect(sql)
        }()

    dispatch_sync(_dbQueue) {
        query
    }

    return recordset
}

When doing some testing however, I got this. Two separate threads running sqlite at the same time. How do I keep this from happening?

exception error shown with stack

Aucun commentaire:

Enregistrer un commentaire