为什么清除领域表的内容会使对象无效? [英] why does clearing contents of realm table invalidate the object?

查看:71
本文介绍了为什么清除领域表的内容会使对象无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于收藏夹的表,我想要做的是清除所有数据表,然后用数组的内容重新加载它.这是代码:

//empty FavouritesRealm table and reload favouritesArray back into FavouritesRealm
    let clearTable = realm.objects(FavouritesRealm)
    try! realm.write{
        for row in clearTable{
            realm.delete(row)
        }

        for f in favouritesArray{
            let favouriteRealm = FavouritesRealm()
            favouriteRealm.name = f.name
            favouriteRealm.price = f.price
            favouriteRealm.dbSource = f.dbSource
            favouriteRealm.date = f.date
            favouriteRealm.favourite = f.favourite
            realm.add(favouriteRealm)
        }
    }

现在,该应用程序崩溃并显示以下注释: 由于未捕获的异常'RLMException'而终止应用程序,原因:'对象已被删除或无效.'

在删除所有行时,Swift似乎删除了我的对象(即表),但我只想清除所有数据.我该如何解决?

解决方案

Realm使用零拷贝方法,在其中检索实时更新的对象访问器,这些对象访问器直接访问内存映射的数据而不是副本.

基于错误消息,我假定favouritesArray中的对象是托管领域对象.如果是这种情况,则无需重新创建它们.只能在写事务中修改托管对象,这意味着所有更改都将保留.与其尝试删除所有对象然后重新添加收藏夹,不如删除那些不再受欢迎的对象,可能会更容易

如果此数组包含尚未添加到Realm中的独立对象和已添加到Realm中的托管对象,则add(:_ update: true)如果它们具有主键,则可以方便地添加或更新对象./p>

I have a table for favourites and what I want to do is to clear the table of all data and then reload it with the contents of an array. Here is the code:

//empty FavouritesRealm table and reload favouritesArray back into FavouritesRealm
    let clearTable = realm.objects(FavouritesRealm)
    try! realm.write{
        for row in clearTable{
            realm.delete(row)
        }

        for f in favouritesArray{
            let favouriteRealm = FavouritesRealm()
            favouriteRealm.name = f.name
            favouriteRealm.price = f.price
            favouriteRealm.dbSource = f.dbSource
            favouriteRealm.date = f.date
            favouriteRealm.favourite = f.favourite
            realm.add(favouriteRealm)
        }
    }

Now, the app crashes with the comment: "Terminating app due to uncaught exception 'RLMException', reason: 'Object has been deleted or invalidated.'"

Swift seems to delete my object (which is the table) when all rows are deleted, but I just want to clear all data. How can I get around this?

解决方案

Realm is using a zero-copy approach where you retrieve live-updating object accessors which directly access memory-mapped data instead of copies.

Based on the error message, I assume that the objects in favouritesArray are managed Realm objects. If that's the case, there would be no need to re-create them. Managed objects can only be modified within write transactions, which implies that all your changes are persisted. Instead of trying to delete all objects and then re-adding the favorites, it might be easier in your case to delete just the objects which are not favored anymore

If this array contains standalone objects, which were not added to a Realm yet and managed objects, which are already added to a Realm, then add(:_ update: true) can facilitate to add or update your objects if they have a primary key.

这篇关于为什么清除领域表的内容会使对象无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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