我对Realm的“级联删除”操作进行了搜索.遗憾的是,该功能尚未实施.我自己实现了它并在这里分享了它.
如何为Realm“级联删除”操作制作通用代码?
解决方法:
1)将此代码复制到您的项目中
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();
}
}
2)为项目添加接口.如果您的Realm对象实现此接口,则在调用deleteCascade后将删除所有子对象.如果接口未实现此功能,则删除Realm对象但不删除子对象.
public interface IRealmCascade {
}
3)声明你的Realm对象.以下示例.
public class NodeModel extends RealmObject implements IRITSerializable, IRealmCascade {
@PrimaryKey
@SerializedName("id") private String objId;
@SerializedName("parentId") private String parentId;
@SerializedName("contentType") private String nodeType;
@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 String getNodeType() {
return nodeType;
}
public void setNodeType(String nodeType) {
this.nodeType = nodeType;
}
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;
}
}
4)你需要调用RealmUtils.deleteCascade(realmObject);而是realmObject.removeFromRealm();
以下示例
更新本地数据库中的数据
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);
}
享受你的干净DB!