Skip to content

Transactions + Protocols Best Practices #1170

Answered by groue
matt-dewitt asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @matt-dewitt,

It looks like you could write working code with unsafeReentrant methods, and this looks fine to me. I expect something like:

struct CounterGRDBInMemoryStorage: CounterStorageProtocol {
    func save(counter: Counter) throws {
        try dbQueue.unsafeReentrantWrite { db in
            try counter.insert(db)
        }
    }

    func doTransaction(_ transactionBlock: () throws -> Void) throws {
        try dbQueue.unsafeReentrantWrite { db in
            try db.inTransaction {
                try transactionBlock()
                return .commit
            }
        }
    }
}

You must use "unsafe" methods, because CounterStorageProtocol does not force the user to thi…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@matt-dewitt
Comment options

Answer selected by matt-dewitt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants