领域数据isValid()为false但可以获取数据

时间:2017-03-06 08:23:52

标签: android realm

我使用Realm在我的项目中存储数据。我有奇怪的问题。 我的模特:

Class Post extends RealmObject implements Serializable{
  @PrimaryKey
  int id;
  ...
  RealmList<Post> subdata;

  RealmList<Post> getSubdata() {
      return subdata;
  }

}

我经常检查getSubdata() != null && getSubdata().isValid()以使用子数据。问题是当我检查getSubdata().isValid()返回false时,我仍然会在subdata中获取数据。我没有检查isValid时没关系。文件说的像这样:

/**
     * Checks if the {@link RealmList} is managed by Realm and contains valid data i.e., the {@link io.realm.Realm}
     * instance hasn't been closed.
     *
     * @return {@code true} if still valid to use, {@code false} otherwise or if it's an unmanaged list.
     */
    public boolean isValid() {
        //noinspection SimplifiableIfStatement
        if (realm == null || realm.isClosed()) {
            return false;
        }
        return isAttached();
    }

更新调试:

https://uk.mathworks.com/help/matlab/math/matrix-indexing.html

1 个答案:

答案 0 :(得分:0)

您的类需要扩展RealmObject。所以它应该改为:

Class Post extends RealmObject {
  @PrimaryKey
  int id;
  ...
  RealmList<Post> subdata;

  RealmList<Post> getSubdata() {
      return subdata;
  }

}