Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何删除迁移期间未被任何其他对象引用的领域对象?_Android_Realm_Realm Java - Fatal编程技术网

Android 如何删除迁移期间未被任何其他对象引用的领域对象?

Android 如何删除迁移期间未被任何其他对象引用的领域对象?,android,realm,realm-java,Android,Realm,Realm Java,我有两种型号- class Direction : RealmObject { var distance : Int var polyline : String } class Route : RealmObject { var id : String var directionList : RealmList<Direction> } 类方向:RealmObject{ 变量距离:Int 变量多段线:字符串 } 类路由:RealmObject{

我有两种型号-

class Direction : RealmObject {
    var distance : Int
    var polyline : String
}

class Route  : RealmObject {
    var id : String
    var directionList : RealmList<Direction>
}
类方向:RealmObject{
变量距离:Int
变量多段线:字符串
}
类路由:RealmObject{
变量id:String
var-directionList:RealmList
}
我一直在使用
insertOrUpdate()
更新Route类,假设在调用它时,directionList中现有的direction对象被删除并替换为我提供的新列表。然而,我最近发现这并没有发生。调用
route.deleteFromRealm()
时,甚至不支持级联删除。现在我在方向表中找到了数百个对象,没有对象引用它们


如何从Direction类中删除那些在领域迁移中没有引用它们的路由对象的所有对象?

我可以想到两种可能的方法

第一种方法目前可能对你没有帮助,但将来可能会有帮助。通过向
方向
类添加LinkingObjects属性,可以让模型确定哪些
方向
对象没有相关的
路由
对象。此处介绍了链接对象()。属性位于
方向上时:例如:

\@LinkingObjects("direction")
final RealmResults<Route> routes = null;
\@LinkingObjects(“方向”)
最终RealmResults routes=null;
然后,您可以删除以下任何对象:

RealmResults<Direction> unusedDirections = realm.where(Direction.class).isEmpty("routes").findAll();
RealmResults unusedDirections=realm.where(Direction.class).isEmpty(“路由”).findAll();
不过,您可能需要在下一版本中执行此操作

第二种方式比较冗长,但本质上:

  • 查找所有
    Direction
    对象:
    RealmResults redundantDirections=realm.where(Direction.class).findAll()
  • 查找所有
    Route
    对象(与上面类似)
  • 遍历所有
    Route
    对象
  • 过滤
    redundantDirections
    查询以排除每个
    Route
    对象引用的任何
    Direction
    对象
  • 删除最后的
    冗余方向

    我希望还有第三种我不知道的方法……

    这就是我解决问题的方法-

    override fun migrate(realm: DynamicRealm, oldVersion1: Long, newVersion: Long) {
    
        if (oldVersion == 2L) {
    
            val routeSchema = schema.get("Route")
            val directionSchema = schema.get("Direction")
    
            /*
            Creating a new temp field called isLinked which is set to true for those which are
            references by Route objects. Rest of them are set to false. Then removing all
            those which are false and hence duplicate and unnecessary. Then removing the temp field
            isLinked
             */
            directionSchema!!
                    .addField("isLinked", Boolean::class.java)
                    .transform { obj ->
                        //Setting to false for all by default
                        obj.set("isLinked", false)
                    }
    
            routeSchema!!.transform { obj ->
                obj.getList("directionList").forEach {
                    //Setting to true for those which are referenced
                    it.set("isLinked", true)
                }
            }
    
            //Removing all those which are set as false
            realm.where("Direction")
                    .equalTo("isLinked", false)
                    .findAll()?.deleteAllFromRealm()
    
            directionSchema.removeField("isLinked")
    
            //Rest of the migration
        }
    }
    
    我发现了更多的东西。根据这篇关于领域(跳到28:45)的内容丰富的演讲,有一种方法可以从B树中删除所有未引用的节点。然而,我找不到一种方法来做到这一点
    Realm.compactRealm()
    似乎是一种实现这一点的方法,但它不起作用

    如何从Direction类中删除所有那些在领域迁移中没有引用它们的路由对象的对象

    应易于使用
    DynamicRealmObject.linkingObjects

    val routes = realm.where("Destination").findAll()
    routes.createSnapshot().forEach { route ->
        val linkingObjects = route.linkingObjects("Route", "directionList")
        if(linkingObjects.isEmpty()) {
            route.deleteFromRealm()
        }
    }
    

    这在理论上应该行得通

    嘿@chris shaw,谢谢你的回答。我有一个后续的疑问。有没有办法在迁移本身中添加这些链接对象?如果有,也许我可以先添加,然后删除那些没有链接的。然而,除了这个用例之外,我对链接对象没有任何其他需要,并且对于我的用例来说似乎是多余的。我以一种粗糙但有效的方式解决了我的问题,有点类似于你在第二种方式中提到的。@abhiank我不知道。如果有人知道,你可以试着问另一个问题。
    DynamicRealmObject
    实际上有一个
    linkingObjects
    方法,这对我来说也是一个惊喜。