Realm 以高效的方式删除孤立对象
Realm Delete Orphaned Objects in an Efficient Way
我有一个 class BeaconBase 包含 BeaconData 列表。
public class BeaconBase extends RealmObject {
@PrimaryKey
private Integer checkInID;
@SerializedName("beacons")
@Expose
private RealmList<BeaconData> beacons;
}
public class BeaconData extends RealmObject {
@SerializedName("scanId")
@Expose
private String scanId;
@SerializedName("distance")
@Expose
private String distance;
}
BeaconData 通常会很大。可能是每小时 100000 行左右。删除相关BeaconBase记录时如何高效删除BeaconData?
我只有 BeaconBase 有主键。
所以当我删除 BeaconBase 记录时,数以千计的 BeaconData 记录被孤立。我怎样才能有效地处理这种情况?
try(Realm r = Realm.getDefaultInstance)) {
r.executeTransaction((realm) -> {
RealmResults<BeaconBase> beaconsToDelete = realm.where(BeaconBase.class)./*query here*/.findAll();
for(int i = beaconsToDelete.size() - 1; i >= 0; i--) {
BeaconBase beaconBase = beaconsToDelete.get(i);
beaconBase.getBeacons().deleteAllFromRealm();
beaconBase.deleteFromRealm();
}
});
}
虽然稍微有点用处,但你也可以这样做:
public class BeaconBase extends RealmObject {
@PrimaryKey
private Integer checkInID;
@SerializedName("beacons")
@Expose
private RealmList<BeaconData> beacons;
}
public class BeaconData extends RealmObject {
@SerializedName("scanId")
@Expose
private String scanId;
@SerializedName("distance")
@Expose
private String distance;
@LinkingObjects("beacons")
private final RealmResults<BeaconBase> beaconDataOf = null;
}
try(Realm r = Realm.getDefaultInstance)) {
r.executeTransaction((realm) -> {
realm.where(BeaconBase.class)./*query here*/.findAll().deleteAllFromRealm();
realm.where(BeaconData.class).isEmpty("beaconDataOf").findAll().deleteAllFromRealm();
});
}
我有一个 class BeaconBase 包含 BeaconData 列表。
public class BeaconBase extends RealmObject {
@PrimaryKey
private Integer checkInID;
@SerializedName("beacons")
@Expose
private RealmList<BeaconData> beacons;
}
public class BeaconData extends RealmObject {
@SerializedName("scanId")
@Expose
private String scanId;
@SerializedName("distance")
@Expose
private String distance;
}
BeaconData 通常会很大。可能是每小时 100000 行左右。删除相关BeaconBase记录时如何高效删除BeaconData?
我只有 BeaconBase 有主键。
所以当我删除 BeaconBase 记录时,数以千计的 BeaconData 记录被孤立。我怎样才能有效地处理这种情况?
try(Realm r = Realm.getDefaultInstance)) {
r.executeTransaction((realm) -> {
RealmResults<BeaconBase> beaconsToDelete = realm.where(BeaconBase.class)./*query here*/.findAll();
for(int i = beaconsToDelete.size() - 1; i >= 0; i--) {
BeaconBase beaconBase = beaconsToDelete.get(i);
beaconBase.getBeacons().deleteAllFromRealm();
beaconBase.deleteFromRealm();
}
});
}
虽然稍微有点用处,但你也可以这样做:
public class BeaconBase extends RealmObject {
@PrimaryKey
private Integer checkInID;
@SerializedName("beacons")
@Expose
private RealmList<BeaconData> beacons;
}
public class BeaconData extends RealmObject {
@SerializedName("scanId")
@Expose
private String scanId;
@SerializedName("distance")
@Expose
private String distance;
@LinkingObjects("beacons")
private final RealmResults<BeaconBase> beaconDataOf = null;
}
try(Realm r = Realm.getDefaultInstance)) {
r.executeTransaction((realm) -> {
realm.where(BeaconBase.class)./*query here*/.findAll().deleteAllFromRealm();
realm.where(BeaconData.class).isEmpty("beaconDataOf").findAll().deleteAllFromRealm();
});
}