写入领域数据库时崩溃 [英] Crashing on writing to realm database

查看:223
本文介绍了写入领域数据库时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

    }

这篇关于写入领域数据库时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆