Skip to content

Thread Safety

Jeff Hurray edited this page Apr 25, 2016 · 1 revision

Most SQLiteModel operations are guaranteed to be thread-safe. However, there are obviously times when thread safety needs to be enforced. When this is the case, it is suggested you use the status transaction method that takes advantage of the internal locking mechanisms of SQLiteModel.

In this example, the transaction is atomic. The gross for empireStrikesBack is guaranteed to be increased by 200. Without the transaction block, the increase could either be 100 or 200.

let concurrentQueue = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT)

dispatch_async(concurrentQueue, {
    Movie.transaction({
        let currentGross = empireStrikesBack => Movie.Gross
        empireStrikesBack <| Movie.Gross |> (currentGross + 100)
    })
})
dispatch_async(concurrentQueue, {
    Movie.transaction({
        let currentGross = empireStrikesBack => Movie.Gross
        empireStrikesBack <| Movie.Gross |> (currentGross + 100)
    })
})

Clone this wiki locally