Created
October 30, 2018 15:21
-
-
Save rubenpla-develop/b3b44e12c90b875afc5bdfd54739b88e to your computer and use it in GitHub Desktop.
Realm Delete Cascade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* You need to call RealmUtils.deleteCascade(realmObject); | |
* instead realmObject.removeFromRealm(); Example below Update data in local database | |
*/ | |
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
2) Add interface to your project. If your Realm object implement this interface all child objects will be deleted | |
after call deleteCascade. If interface not implemented this function delete Realm object but don't delete child objects. | |
*/ | |
public interface IRealmCascade { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
1) Copy this code to your project | |
*/ | |
import android.util.Log; | |
import java.lang.reflect.Method; | |
import io.realm.RealmList; | |
import io.realm.RealmObject; | |
import 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment