我有一个待办事项应用程序,正在使用领域来存储数据.我已经编写了用于写入数据库和检索的数据库代码.
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) }更多推荐
写入领域数据库时崩溃
发布评论