【发布时间】:2017-05-04 02:46:02
【问题描述】:
我将 SQLite 用于我的持久存储。
我使用基于主键的字典来存储内存:var localContext = [String: GrandLite]()。
我使用下面的函数从字典中检索对象,或者从数据库中检索对象,然后存储在字典中。这个函数被频繁调用,我正在尝试优化它。
class func retrieveByKey<T: GrandLite>(aType: [T], thisKey: String) -> T? {
let thisStack = FoodysLiteStack.thisDataStack
if let thisObject = thisStack.localContext[thisKey] as? T {
return thisObject
} else {
do {
let db = thisStack.localDatabase
let thisTable = T.getTable()
if let thisRow = try db.pluck(thisTable.filter(uuidKeyLite == thisKey)) {
let thisObject = T(withRow: thisRow)
thisStack.localContext[thisKey] = thisObject
return thisObject
} else {
return nil
}
} catch {
NSLog("WARNING: Unhandled error for T retrieveByKey")
return nil
}
}
}
据我了解 sqlite.swift pluck 基本上是一个限制为 1 的准备。此外,准备编译 SQL 语句,绑定变量并执行它。每次调用此函数时,我都试图避免 SQLite 编译时间。
在 sqlite.swift 框架中,有没有一种方法可以提前准备好语句,然后在执行前绑定变量?您可以使用 db.prepare 和 db.run 对插入和更新执行此操作,但我看不到为已经准备好的 select 语句绑定变量的方法。我可能想多了,thisTable.filter(uuidKeyLite == thisKey) 上的 SQLite 编译时间可能很小。
【问题讨论】:
标签: ios sqlite sqlite.swift