ios - 使用事务插入会引发错误 Sqlite.swift
<p><p>我已经创建了一个数据库类:</p>
<pre><code>class Database {
static let instance = Database()
private let categories = Table("Category")
private var db: Connection?
let cat_id = Expression<String>("id")
let cat_name = Expression<String>("name")
private init() {
let path = NSSearchPathForDirectoriesInDomains(
.documentDirectory, .userDomainMask, true
).first!
do {
db = try Connection("\(path)/SalesPresenterDatabase.sqlite3")
createTable()
} catch {
print("error")
}
}
func createTable() {
do{
try self.db!.run(self.categories.create(ifNotExists: true) { table in
table.column(self.cat_id)
table.column(self.cat_name)
})
}catch{
print("error")
}
}
func addRow(table: DBTableNames, object: ) -> Int64? {
do {
try self.db!.transaction() {
for obj in object{
if table.rawValue == DBTableNames.Category.rawValue{
let cats : CategoryObject = obj as! CategoryObject
let insert = self.categories.insert(self.cat_id <- cats.id,
self.cat_name <- cats.name)
try self.db!.run(insert)
}
}
}
}catch{
print("Insert failed \(error)")
return -1
}
return 1
}
}
</code></pre>
<p>然后我继续调用我的代码,通过运行以下命令添加一行:</p>
<pre><code> let returnValue = Database.instance.addRow(table: DBTableNames(rawValue: entity)!,
object: databaseObject)
</code></pre>
<p>我的问题是它总是抛出一个错误说:</p>
<blockquote>
<p>Insert failed The operation couldn’t be completed. (SQLite.Result
error 0.) and full: cannot rollback - no transaction is active (code:
1)</p>
</blockquote>
<p>如果我再次看到此错误,我的 Mac 将会弹出窗口!</p>
<p>整个操作在后台线程中,但我也尝试了以下操作:</p>
<pre><code> DispatchQueue.main.async{
let returnValue = Database.instance.addRow(table: DBTableNames(rawValue: entity)!,
object: databaseObject)
}
</code></pre>
<p>没有用。这没有任何意义,因为我也尝试在事务中创建表并且效果很好。 </p>
<p>任何帮助将不胜感激!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您的 <code>func createTable()</code> 和 <code>func addRow()</code> 方法不是线程安全的。多个线程可以同时访问它。</p>
<p>在您的 Singleton 类中创建一个私有(private)串行 <code>DispatchQueue</code> 并通过此串行队列执行上述功能。这样可以防止多个线程同时访问数据库,串行队列会将并发任务排队。</p></p>
<p style="font-size: 20px;">关于ios - 使用事务插入会引发错误 Sqlite.swift,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/43388443/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/43388443/
</a>
</p>
页:
[1]