写入领域数据库时崩溃

编程入门 行业动态 更新时间:2024-10-23 21:39:19
本文介绍了写入领域数据库时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个待办事项应用程序,正在使用领域来存储数据.我已经编写了用于写入数据库和检索的数据库代码.

I have a todo application which I am using realm to store data. i have written the database codes for writing to the database and retrieve.

当我第一次输入数据库时​​,一切正常,这就是将数据输入到数据库中,但是当我尝试将另一个对象输入到数据库中时,

When I input into the database for the first time, everything works well that is the data gets inputted into the database but when I try to input another object into the databse,

由于未捕获的异常"RLMException"而终止应用程序,原因:试图在写事务之外修改对象-首先在RLMRealm实例上调用beginWriteTransaction."

Terminating app due to uncaught exception 'RLMException', reason: 'Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.'

func createCategory(name: String, color: String, isCompleted: Bool) -> Void { category.name = name category.color = color category.isCompleted = false DBManager.instance.addData(object: category) }

DBManager

func addData(object: CategoryModel) { try! database.write { database.add(object, update: true) print("Added new object") } }

添加类别IBaction

CategoryFunctions.instance.createCategory(name: name, color: color, isCompleted: false)

推荐答案

.createCategory(name: name, color: color, isCompleted: false)

更改已添加对象的属性,然后尝试再次写入它,您需要创建一个新对象,也许您需要类似

changes the properties of already added object and try to write it again , you need to create a new object , may be you need something like

func createCategory(name: String, color: String, isCompleted: Bool) -> Void { let category = Category() category.name = name category.color = color category.isCompleted = isCompleted DBManager.instance.addData(object: category) // or let category = Category(name:name,color:color,isCompleted:isCompleted) DBManager.instance.addData(object: category) }

更多推荐

写入领域数据库时崩溃

本文发布于:2023-10-28 19:39:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537591.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:领域   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!