gpt4 book ai didi

swift - 如何从 GRDB 中的只读数据库连接中调用新的数据库连接?

转载 作者:行者123 更新时间:2023-11-28 05:40:27 28 4
gpt4 key购买 nike

下面的函数返回一个 Ledgers 记录。大多数时候,它会在可选的 _currentReceipt 变量中找到它,或者通过搜索数据库找到它,不需要在那里写入。我想使用只读 GRDB 数据库连接。只读数据库连接可以在不同线程上并行运行。

在极少数情况下,前两个步骤失败,我可以创建一个默认的分类帐。调用 try FoodyDataStack.thisDataStack.dbPool.write { writeDB in ... 将引发 fatal error ,数据库连接不可重入。我正在寻找一种方法来保存默认分类帐,而不必将整个函数包装在读写连接中。

我可以在 GRDB .read block 中的单独队列上调用 NSOperation 吗?

class func getCurrentReceipt(db: Database) throws -> Ledgers {
if let cr = FoodyDataStack.thisDataStack._currentReceipt {
return cr
}
// Fall through
do {
if let cr = try Ledgers.filter(Ledgers.Columns.receiptClosed == ReceiptStatus.receiptOpen.rawValue).order(Ledgers.Columns.dateModified.desc).fetchOne(db) {
FoodyDataStack.thisDataStack._currentReceipt = cr
return cr
} else {
throw FoodyDataStack.myGRDBerrors.couldNotFindCurrentReceipt
}
} catch FoodyDataStack.myGRDBerrors.couldNotFindCurrentReceipt {
// Create new receipt with default store
let newReceipt = Ledgers()
newReceipt.dateCreated = Date()
newReceipt.dateModified = Date()
newReceipt.receiptStatus = .receiptOpen
newReceipt.receiptUrgency = .immediate
newReceipt.dateLedger = Date()
newReceipt.uuidStore = Stores.defaultStore(db).uuidKey
FoodyDataStack.thisDataStack._currentReceipt = newReceipt
return newReceipt
} catch {
NSLog("WARNING: Unhandled error in Ledgers.getCurrentReceipt() \(error.localizedDescription)")
}
}

编辑:我把这个问题留在这里,但我想我可能会进行过早的优化。我将尝试使用 dbQueue 而不是 dbPool,看看性能如何。如果速度需要,我会回到 dbPool。

最佳答案

GRDB数据库访问方式不可重入(DatabaseQueue和DatabasePool的读写方式)。

为了帮助您解决问题,请尝试将您的数据库访问方法分为两个级别。

第一层不向应用程序的其余部分公开。它的方法都采用 db: Database 参数。

class MyStack {
private var dbQueue: DatabaseQueue

private func fetchFoo(_ db: Database, id: Int64) throws -> Foo? {
return try Foo.fetchOne(db, key: id)
}

private func setBar(_ db: Database, foo: Foo) throws {
try foo.updateChanges(db) {
$0.bar = true
}
}
}

第二层的方法暴露给应用程序的其余部分。它们将一级方法包装在 readwrite 数据库访问方法中:

class MyStack {
func fetchFoo(id: Int64) throws -> Foo? {
return try dbQueue.read { db in
try fetchFoo(db, id: id)
}
}

func setBar(id: Int64) throws {
try dbQueue.write { db in
guard let foo = try fetchFoo(db) else {
throw fooNotFound
}
try setBar(foo: foo)
}
}
}

第一层的方法可以是低层的,可以组合

第二层的方法是高层的,不可组合:它们不能相互调用,因为“数据库连接不可重入” fatal error 。它们提供保证数据库一致性的线程安全数据库方法。

参见 Concurrency Guide获取更多信息。

关于swift - 如何从 GRDB 中的只读数据库连接中调用新的数据库连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56943774/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com