多对多关系领域

时间:2016-10-22 19:58:03

标签: android realm

我的目标是在Realm的同一个班级内建立多对多的关系。我试图以这种方式向RealmList添加对象:

Realm realm = Realm.getDefaultInstance();
            DeviceEntity device = realm.where(DeviceEntity.class).equalTo(Constants.DEVICE_PARAM_DEVICE_ID, deviceId).findFirst();
            DeviceEntity deviceToBeAdded = realm.where(DeviceEntity.class).equalTo(Constants.DEVICE_PARAM_DEVICE_ID, deviceIdToAdd).findFirst();

            if(device != null && deviceToBeAdded != null){

                realm.beginTransaction();

                device.getConnectedDevices().add(deviceToBeAdded);
                realm.copyToRealmOrUpdate(device);
                realm.commitTransaction();


            }

当我尝试获取存储在领域中的所有DeviceEntity对象时:

 Realm realm = Realm.getDefaultInstance();
            final RealmResults<DeviceEntity> realmResults = realm
                    .where(DeviceEntity.class)
                    .findAll();


            List<DeviceEntity> list = Arrays.asList(realmResults.toArray(new DeviceEntity[realmResults.size()]));

对于所有DeviceEntity对象,getConnectedDevices()返回null

public class DeviceEntity extends RealmObject{

@PrimaryKey
private long id;
private String name;
private String deviceId;
private String deviceType;
private RealmList<DeviceEntity> connectedDevices = new RealmList<>();

public DeviceEntity() {

}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getName() {
    return name;
}

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

public String getDeviceId() {
    return deviceId;
}

public void setDeviceId(String deviceId) {
    this.deviceId = deviceId;
}

public String getDeviceType() {
    return deviceType;
}

public void setDeviceType(String deviceType) {
    this.deviceType = deviceType;
}

public RealmList<DeviceEntity> getConnectedDevices() {
    return connectedDevices;
}

public void setConnectedDevices(RealmList<DeviceEntity> connectedDevices) {
    this.connectedDevices = connectedDevices;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    DeviceEntity that = (DeviceEntity) o;

    if (id != that.id) return false;
    if (name != null ? !name.equals(that.name) : that.name != null) return false;
    if (deviceId != null ? !deviceId.equals(that.deviceId) : that.deviceId != null)
        return false;
    if (deviceType != null ? !deviceType.equals(that.deviceType) : that.deviceType != null)
        return false;
    return connectedDevices != null ? connectedDevices.equals(that.connectedDevices) : that.connectedDevices == null;

}

@Override
public int hashCode() {
    int result = (int) (id ^ (id >>> 32));
    result = 31 * result + (name != null ? name.hashCode() : 0);
    result = 31 * result + (deviceId != null ? deviceId.hashCode() : 0);
    result = 31 * result + (deviceType != null ? deviceType.hashCode() : 0);
    result = 31 * result + (connectedDevices != null ? connectedDevices.hashCode() : 0);
    return result;
}

public static int getNextPrimaryKey(Realm realm){
    int key;
    try {
        Number number = realm.where(DeviceEntity.class).max("id");
        key = number != null ? (number.intValue() + 1) : 1;
    } catch(ArrayIndexOutOfBoundsException ex) {
        key = 1;
    }

    return key;
}

}

我该如何获取connectedDevices?

编辑:

我尝试实现链接但也没有效果,从db获取后,RealmList仍为null。

final Realm realm = Realm.getDefaultInstance();
                final DeviceEntity device = realm.where(DeviceEntity.class).equalTo(Constants.DEVICE_PARAM_DEVICE_ID, deviceId).findFirst();
                final DeviceEntity deviceToBeAdded = realm.where(DeviceEntity.class).equalTo(Constants.DEVICE_PARAM_DEVICE_ID, deviceIdToAdd).findFirst();

                try {
                    realm.executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {

                            if(device != null && deviceToBeAdded != null){

                                device.getConnectedDevices().add(deviceToBeAdded);
                                realm.insertOrUpdate(device);

                                subscriber.onCompleted();
                            }

                        }
                    });
                } finally {
                    if(realm != null) {
                        realm.close();
                    }
                }

0 个答案:

没有答案