【问题标题】:Adding a RealmObject to a RealmList in Android在 Android 中将 RealmObject 添加到 RealmList
【发布时间】:2021-03-14 18:07:48
【问题描述】:

我目前正在尝试将一个 RealmObject 添加到另一个 RealmObject 中的 RealmList。

这就是我目前的做法。

首先,我创建并保存一个名为“RouteRealm”的 RealmObject,如下所示:

public void insertNewRoute(int routeId, long routeDate) {
    realm.beginTransaction();
    RouteRealm routeRealm = realm.createObject(RouteRealm.class);
    routeRealm.setId(routeId);
    routeRealm.setDate(routeDate);
    realm.commitTransaction();
}

RealmObject 类如下所示:

public class RouteRealm extends RealmObject {

    @PrimaryKey
    private int id;
    private long date;
    private RealmList<RoutePointRealm> routePoints;
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public long getDate() {
        return date;
    }

    public void setDate(long date) {
        this.date = date;
    }

    public RealmList<RoutePointRealm> getRoutePoints() {
        return routePoints;
    }

    public void setRoutePoints(RealmList<RoutePointRealm> routePoints) {
        this.routePoints = routePoints;
    }
}

以上工作。当我尝试将 RoutePointRealm 添加到名为 routePoints 的列表时,就会出现问题。这是我将 RoutePointRealm 对象添加到列表的代码:

public void insertNewRoutePoint(int routeId, String address, float latitude, float longitude, long routePointId, long routePointTime) {
        realm.beginTransaction();
        RouteRealm routeRealm = realm.where(RouteRealm.class).equalTo("id", routeId).findFirst();

        RoutePointRealm routePointRealm = realm.createObject(RoutePointRealm.class);
        routePointRealm.setAddress(address);
        routePointRealm.setLatitude(latitude);
        routePointRealm.setLongitude(longitude);
        routePointRealm.setRoutePointID(routePointId);
        routePointRealm.setRoutepointTime(routePointTime);
        routeRealm.getRoutePoints().add(routePointRealm);
        realm.copyToRealmOrUpdate(routeRealm);

        realm.commitTransaction();
    }

RoutePointRealm 看起来像这样:

public class RoutePointRealm extends RealmObject {

    @PrimaryKey
    private long     routePointID;
    private float     longitude, latitude;
    private long     routepointTime;
    private String  address;

    public long getRoutePointID() {
        return routePointID;
    }

    public void setRoutePointID(long routePointID) {
        this.routePointID = routePointID;
    }

    public float getLongitude() {
        return longitude;
    }

    public void setLongitude(float longitude) {
        this.longitude = longitude;
    }

    public float getLatitude() {
        return latitude;
    }

    public void setLatitude(float latitude) {
        this.latitude = latitude;
    }

    public long getRoutepointTime() {
        return routepointTime;
    }

    public void setRoutepointTime(long routepointTime) {
        this.routepointTime = routepointTime;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

由于某种原因,RoutePointRealm 被添加到列表中,但它的所有字段都设置为零和空。我已经调试了我的应用程序以确保所有参数都包含具有正确数据类型等的正确值。所以现在我知道问题与 Realm 方法有关。

我做错了什么?

【问题讨论】:

    标签: android realm


    【解决方案1】:

    首先感谢您的回答!更改您提出的解决方案后,我仍然无法使其正常工作。至少我不这么认为。

    我认为它不起作用的原因,部分是因为我的 gui 显示零值时犯了一个错误。这让我开始调试应用程序,但显然调试器总是显示零或 null Realm 对象的值.. 至少在我的情况下。 所以最后,我尝试使用从 Realm 获取的值制作一条 Toast 消息,它返回了应有的值。

    所以我认为一开始没有任何问题。调试器让我这么想。很抱歉浪费了您的时间,但我仍然认为如果其他人遇到同样的问题,我应该将其发布为答案。

    【讨论】:

      【解决方案2】:

      您的所有对象都由 Realm 管理,因此您不需要 realm.copyToRealmOrUpdate(routeRealm); 调用。

      【讨论】:

        【解决方案3】:

        问题来自 ID,Realm 不支持自动递增行为,因此您应该手动执行。 类似:

        beginTransaction()
        foo.setId(value)
        commitTrasaction()
        

        【讨论】:

          【解决方案4】:

          我的个人建议:

          public void insertNewRoute(final int routeId, final long routeDate) {
              final RouteRealm routeRealm = new RouteRealm();
              realm.executeTransaction(new Realm.Transaction() {
                  @Override
                  public void execute(Realm realm) {
                      routeRealm.setId(routeId);
                      routeRealm.setDate(routeDate);
                      realm.insertOrUpdate(routeRealm);    
                  }
              });
          }
          
          public void insertNewRoutePoint(final int routeId, final String address, final float latitude, 
                                 final float longitude, final long routePointId, final long routePointTime) {
              realm.executeTransaction(new Realm.Transaction() {
                  @Override
                  public void execute(Realm realm) {
                      RouteRealm routeRealm = realm.where(RouteRealm.class).equalTo(RouteRealmFields.ID, routeId).findFirst();
                      RoutePointRealm routePointRealm = new RoutePointRealm();
                      routePointRealm.setAddress(address);
                      routePointRealm.setLatitude(latitude);
                      routePointRealm.setLongitude(longitude);
                      routePointRealm.setRoutePointID(routePointId);
                      routePointRealm.setRoutepointTime(routePointTime);
                      routePointRealm = realm.copyToRealmOrUpdate(routePointRealm);
                      routeRealm.getRoutePoints().add(routePointRealm);
                  }
              });
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多