-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Wrapping for PromiseKit #915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I was able to do it by wrapping it into a class that has its own queue. Basically at the beginning there will be some main context and each transactions spawns its own context with its own queue. It uses the same connection across the contexts. Also I would implement other methods on the context like import Foundation
import SQLite
import PromiseKit
class DbContext {
let connection: Connection
let queue: DispatchQueue
let parent: DbContext?
init(with conn: Connection, parent: DbContext? = nil) {
self.connection = conn
self.parent = parent
self.queue = DispatchQueue(
label: "com.zdnkt.robert.DbConext",
qos: .default,
attributes: [],
autoreleaseFrequency: .workItem,
target: nil
)
}
convenience init(parent: DbContext) {
self.init(with: parent.connection, parent: parent)
}
@discardableResult
func transaction(_ closure: @escaping (DbContext) throws -> Promise<Void>) -> Promise<Void> {
return Promise<Void> { resolver in
queue.async {
let childContext = DbContext(parent: self)
do {
try childContext.connection.run("BEGIN DEFERRED TRANSACTION")
} catch {
resolver.reject(error)
return // Stop here since we cannot establish transaction
}
do {
try closure(childContext).wait()
try childContext.connection.run("COMMIT TRANSACTION")
resolver.fulfill(())
} catch {
#warning("TODO: fix optional try")
try? childContext.connection.run("ROLLBACK TRANSACTION")
resolver.reject(error)
}
}
}
}
} |
You can use .map and .then in the Promise.
|
Linked to #937 |
The only reactive integration which makes sense at this point is the standard Combine framework. |
Uh oh!
There was an error while loading. Please reload this page.
Hello, I am trying to create an API that would allow me to simply use SQLite with Promies from PromiseKit.
I encountered and issue with transactions tho, I you image that every call to
run(..)
is returning a Promise, then alsotransaction()
needs to return a Promise in the end I'd say.Is there any example or existing code that shows how to approach this with SQLite?
I was unable to find a way to synchronize it easily.
The text was updated successfully, but these errors were encountered: