我正在删除Realm对象,并收到此错误:
由于未捕获的异常“ RLMException”而终止应用程序,原因:“只能在写事务中添加,删除或创建领域中的对象-首先在RLMRealm实例上调用beginWriteTransaction。”
我尝试过refresh()
extension Realm {
func addWord(_ word: RelatedWord) {
do {
try self.write {
self.add(word)
}
} catch let error {
print(error.localizedDescription)
}
}
func deleteWord(_ word: RelatedWord) {
BG {
do {
self.beginWrite()
self.delete(word)
try self.commitWrite()
//self.refresh()
} catch let error {
print(error.localizedDescription)
}
self.refresh()
}
}
}
VC:
realm.delete(word)
预期结果: 滑动以从Tableview中删除对象
错误:对象删除未正确处理。
***由于未捕获的异常“ RLMException”而终止应用程序,原因:“只能在写入事务中添加,删除或创建领域中的对象-首先在RLMRealm实例上调用beginWriteTransaction。”
我认为问题不在于删除领域对象的功能,而是领域对象和试图访问已删除对象的表视图之间存在不一致。
// Swipe to delete cell and word
func tableView(_ tableView: UITableView,trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let action = UIContextualAction(style: .normal, title: "Delete", handler: { (action, view, completionHandler) in
// Update data source when user taps action
let letters = DataSingleton.shared.relatedArr.keys.sorted()
if let data = DataSingleton.shared.relatedArr[letters[indexPath.section]]?.sorted(by: {$0.word < $1.word}) {
let word = data[indexPath.row]
print(word.word)
self.realm.delete(word)
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.reloadRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
}
completionHandler(true)
})
答案 0 :(得分:0)
我不确定BG是什么,但是我会这样做:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services
.AddEntityFrameworkSqlServer()
.AddDbContext<EmployeeContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("EmployeeContext"));
});