Android Realm-findAll返回带有空参数的对象 [英] Android Realm - findAll returns objects with null parameters
问题描述
我需要在Realm中进行简单的查询,检索MyModel对象的列表,然后在我的应用程序中的其他地方使用它.碰巧,当我查询Realm时,每个对象都有空值,但是 toString 返回期望值.
I need to do a simple query in Realm, retrieve a list of MyModel object and later use it somewhere else in my app. It happens that once I query Realm, each object has null values, but the toString returns the expected values.
型号:
@RealmClass
public class MyModel extends RealmObject implements Serializable {
public static final String KEY_MODEL = "key_myModel";
@PrimaryKey
private int id;
private String myStr;
private int myInt;
//.... getters and setters
@Override
public String toString() {
return "id = " + id
+ "\nmyStr = " + myStr
+ "\nmyInt = " + myInt;
}
}
如何存储值:
public static void storeModel(MyModel model) {
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealm(model);
realm.commitTransaction();
}
如何检索对象:
public static RealmList<MyModel> getNewElementsFromIndex(int indexFrom) {
Realm realm = Realm.getDefaultInstance();
RealmResults<MyModel> allValues = realm.where(MyModel).greaterThan("id", indexFrom).findAll();
RealmList<MyModel> finalList = new RealmList<MyModel>();
finalList.addAll(allValues.subList(0, allValues.size()));
return finalList;
}
当我调用 getNewElementsFromIndex(value)时,我会得到一个项目列表,但是此列表中的所有项目都具有参数 myStr = null 和 myInt = 0 .
When i call getNewElementsFromIndex(value) i get a list of item, but all items in this list have the parameter myStr = null and myInt = 0.
我在做什么错了?
推荐答案
对于托管领域对象,数据不会复制到字段中,而是通过代理getter/setter调用获得它们.
For managed realm objects, data is not copied to the fields, you obtain them through the proxy getter/setter calls.
因此,字段为null且toString()显示值的事实是完全可以预期的,并且行为有据可查.
Therefore, the fact that fields are null and toString() shows the values is completely expected and well-documented behavior.
要查看值,必须为getter方法添加监视.
To see the values, you have to add watches for the getter methods.
请参见文档.
这篇关于Android Realm-findAll返回带有空参数的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!