realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

"Cascade delete" implementation

Open laindow opened this issue 10 years ago • 23 comments

Hello guys

Goal

It would be great to have "cascade delete" operation.

Expected Results

When I call Realm.removeFromRealm() function I wanna delete current Realm object and all child Realm objects.

Actual Results

At this stage if I delete Realm object all child objects stay in DB.

Code Sample

I am already made some code and happy to share it with you to make your great framework even better. May be it will be useful for someone.

Interface to make "cascade delete" optional

public interface IRealmCascade {
}

Generic "cascade delete" function

import android.util.Log;


import java.lang.reflect.Method;
import io.realm.RealmList;
import io.realm.RealmObject;
import com.company.project.models.IRealmCascade;

/**
 */

public class RealmUtils
{
public static void deleteCascade( RealmObject dataObject )
{
    if (dataObject == null)
    {
        return;
    }
    if( IRealmCascade.class.isAssignableFrom( dataObject.getClass() ) )
    {
        for( Method method : dataObject.getClass().getSuperclass().getDeclaredMethods() )
        {
            try {
                //Ignore generated methods
                if( (method.getName().contains("realmGet$")) || (method.getName().contains("access$super")) )
                {
                    continue;
                }
                Class<?> resultType = method.getReturnType();
                //Ignore non object members
                if (resultType.isPrimitive()) {
                    continue;
                }

                if (RealmObject.class.isAssignableFrom(resultType)) {
                    //Delete Realm object
                    try {
                        RealmObject childObject = (RealmObject) method.invoke(dataObject);
                        RealmUtils.deleteCascade(childObject);
                    } catch (Exception ex) {
                        Log.e("REALM", "CASCADE DELETE OBJECT: " + ex.toString());
                    }
                } else if (RealmList.class.isAssignableFrom(resultType)) {
                    //Delete RealmList items
                    try {
                        RealmList childList = (RealmList) method.invoke(dataObject);
                        while( childList.iterator().hasNext() )
                        {
                            RealmObject listItem = (RealmObject)childList.iterator().next();
                            RealmUtils.deleteCascade(listItem);
                        }
                    } catch (Exception ex) {
                        Log.e("REALM", "CASCADE DELETE LIST: " + ex.toString());
                    }
                }
            }
            catch (Exception ex)
            {
                Log.e("REALM", "CASCADE DELETE ITERATION: " + ex.toString());
            }
        }
    }
    dataObject.deleteFromRealm();
}

}

Model example

public class NodeModel extends RealmObject implements IRITSerializable, IRealmCascade {
    @PrimaryKey
    @SerializedName("id") private String objId;
    @SerializedName("parentId") private String parentId;
    @SerializedName("realmType") private RealmType realmType;
    @Required
    @SerializedName("name") private String name;

    @SerializedName("settings") private RealmList<ValueTypeModel> columns;

    public String getObjId() {
        return objId;
    }

    public void setObjId(String objId) {
        this.objId = objId;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public RealmType getRealmType() {
        return realmType;
    }

    public void setRealmType(RealmType realmType) {
        this.realmType = realmType;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public RealmList<ValueTypeModel> getColumns() {
        return columns;
    }

    public void setColumns(RealmList<ValueTypeModel> columns) {
        this.columns = columns;
    }
}

"How to use?"

for( NodeModel nodeItem: incomingData.getNodesList() )
{
    RealmResults<NodeModel> results = bgRealm.where(NodeModel.class).equalTo("objId", nodeItem.getObjId()).findAll();
    if (results.size() > 0)
    {
        RealmUtils.deleteCascade(results.first());
    }
    bgRealm.copyToRealm(nodeItem);
}

Version of Realm and tooling

Realm version(s): 0.89

Android Studio version: 2.1

Which Android version and device: Any

laindow avatar May 02 '16 23:05 laindow

Hi @laindow Thank you for your suggestion. Instead of using reflection it might be easier to make a special method in your model classes that does this instead:

public class Foo extends RealmObject {
  public RealmList<Foo> list;
  public String name;

  public void cascadeDelete() {
    list.deleteAllFromRealm(); // The cascade part
    deleteFromRealm(); // delete this object
  }  
}

Your approach is more generic though, which also has a lot of merrit. This is already being tracked in #1104 though, so i'll close this as a duplicate.

cmelchior avatar May 03 '16 20:05 cmelchior

Hi guys,

This is a general solution. You don't need to implement delete function for each model. It can delete your whole objects structure even if it complicated. If you add a property to the model you don't need to rewrite delete function. Sadly payment for that is performance. Reflection always more slow if compare with direct call but if you don't have huge amount of records and your models structure complicated it would be better solution.

Best regards, Konstantin.

laindow avatar May 03 '16 21:05 laindow

@cmelchior Your suggested solution won't work correctly if Foo contains any Realm Models. They'll be orphaned when their parent Foo is deleted right?

snowpong avatar Oct 25 '16 08:10 snowpong

Yes, that is correct. You will need to manually delete all references in the cascadeDelete method.

public class Foo extends RealmObject {
  public RealmList<Foo> list;
  public Bar bar;
  public String name;

  public void cascadeDelete() {
    list.deleteAllFromRealm(); // The cascade part
    if (bar != null) {
      bar.deleteFromRealm();
    }
    deleteFromRealm(); // delete this object
  }  
}

cmelchior avatar Oct 25 '16 08:10 cmelchior

What about when updating an existing nested realmObject with copyToRealmOrUpdate()?

fryossi avatar Nov 09 '16 20:11 fryossi

@fryossi if I know correctly, copyToRealmOrUpdate() goes through all objects in the hierarchy - although technically if you modify a managed RealmObject, then you don't have to call copyToRealmOrUpdate() on it to update its fields.

Zhuinden avatar Nov 09 '16 21:11 Zhuinden

@fryossi If you call copyToRealmOrUpdate() on an existing root object, it will overwrite the child objects with their new data. Note: You'll have orphaned children if the updated root object no longer refers to them. For example Person "Bob" refers to two Dogs, "Fox" and "Rex". But then "Fox" dies and when you call copyToRealmOrUpdate() with a new version of "Bob" you'll still have "Fox" dangling in Realm but "Bob" no longer refers to it.

snowpong avatar Nov 10 '16 08:11 snowpong

You'll have orphaned children if the updated root object no longer refers to them

@Zhuinden and @snowpong - I know, that's the problem! There's a workaround for deletion but how to avoid it on updating with copyToRealmOrUpdate()?

fryossi avatar Nov 10 '16 08:11 fryossi

if you modify a managed RealmObject, then you don't have to call copyToRealmOrUpdate() on it to update its fields

Zhuinden avatar Nov 10 '16 09:11 Zhuinden

@fryossi assuming you have a new "Bob" gotten from a REST server for example, to update it do this in a Transaction:

  1. get the old "Bob" from realm, like: realm.where(Person.class).equalTo("id", newBob.getId()).findFirst()
  2. delete all it's children, like: oldBob.getDogs().deleteAllFromRealm()
  3. insert the new "Bob" like: realm.copyToRealmOrUpdate(newBob)

Note: This assumes the dogs can only have one owner.

snowpong avatar Nov 10 '16 09:11 snowpong

So you are saying there's no option but delete its children.

fryossi avatar Nov 10 '16 09:11 fryossi

@fryossi A more elegant solution is to remove only the children that are no longer referred to in the updated parent. You could possibly do something like:

oldBob.getDogs().where().not().in("id", arrayOfDogIdsFromNewBob).deleteFromRealm()

before calling copyToRealmOrUpdate(newBob)

snowpong avatar Nov 10 '16 09:11 snowpong

Thanks.

fryossi avatar Nov 10 '16 10:11 fryossi

In a situation when many People can refer the same Dogs in their lists, removing lost Dogs may act like GC and can be quite time-consuming 🕔 😒

Miha-x64 avatar Jan 18 '17 14:01 Miha-x64

i don't get what is the deal here. While you can add object and its sub objects without problem, why can't you delete them? You have the index of sub objects ?!

jemshit avatar Oct 26 '17 07:10 jemshit

I think Realm-Core does have support for cascading now.

The question here of course is that if you delete the parent object with RealmResults.deleteAllFromRealm(), then its connected objects should also be automatically deleted by Realm.

That'll happen eventually.

Zhuinden avatar Oct 26 '17 08:10 Zhuinden

Maybe you are right, have not tried on latest version, didn't work on 3.2.0 (a bit old)

jemshit avatar Oct 26 '17 09:10 jemshit

No it is not supported by the Java binding yet, still manual

Zhuinden avatar Oct 26 '17 09:10 Zhuinden

Since after so many years there is still no native approach for cascading deletes, could the Realm Java team at least please provide a temporary generic solution based on the latest version of Realm? Something like what @laindow provides in this issue's description, but updated for the latest version of the library in both Java and Kotlin?

The Realm Swift community (not the actual development team) has come up with some pretty decent solutions over the years.

Some of our projects have tens of very large model types that get updated frequently. Do we have to force our team to update both the migration code AND the cascade deletion code? It's a very error-prone procedure.

I think this problem should not be ignored any more. Most Android and iOS projects I've seen based on Realm just don't implement cascade deletes at all! Both the API and Realm's "simple to use" promotion is deceiving many developers into thinking that the current model delete methods actually clean up everything!

I personally believe that cascade deletes should have been available ages ago, but I understand that resources are limited. However, a temporary "official" generic approach should be prominently available in the documentation until the native solution becomes available... I can't believe that your best suggestion is to have us implement this manually for each one of our model types.

goa avatar Feb 18 '20 00:02 goa

Both the API and Realm's "simple to use" promotion is deceiving many developers into thinking that the current model delete methods actually clean up everything!

Did they ever really say that? You delete an object, nobody auto-clears the linked objects. Imagine this, if the owner of a dog is deleted, the dog lives on.

The best way to avoid hard-to-manage cascade deletion is to use as low number of links as possible. For example, forward-facing uni-directional links can be merged into the same object, inheriting the fields from the supposed linked class.

I am still not a member of Realm, but my links have never been so hard to manage. Worst thing that happened was 3 long but that was unavoidable as they were all lists from one to another.

I would think lately, using @LinkingObjects can verify that there are no linked objects pointing to you, so you can safely remove your object.

I also think that Realm-Core has supported this since 1.5 years ago, but I think Realm Sync got in the way of eventually ever adding the concept of @StrongRelation. A shame that Sync held back the development of the Database.

Zhuinden avatar Feb 18 '20 00:02 Zhuinden