作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设异步API调用返回Member Data,它是一个RealmObject,并且Member被保存到realm中。
public class Member extends RealmObject {
String name;
}
现在我想将 RealmObjectChangeListener 添加到成员对象(例如成员),以便我们可以在数据可用时更新 UI。但是,首先,Member 表为空,member 为 null,因为用于获取成员数据的 API 调用是异步的。将 RealmObjectChangeListener 添加到成员将导致 NullPointerException。遇到这种情况该如何处理呢?
最佳答案
如果您正在监听所有成员,那么很简单:保留对成员的字段引用(在 RecyclerView 的 ViewHolder 中很容易)并将 RealmObjectChangeListener 添加到从 results.get()
获取的成员中,绑定(bind)到 ViewHolder。
void bind(Member member) {
if(this.member != null) {
this.member.removeAllChangeListeners();
}
this.member = member;
this.member.addChangeListener(...);
<小时/>
假设您正在等待具有特定名称
的特定成员
,并且您只关心该成员:
private final Handler handler = new Handler(Looper.getMainLooper());
private RealmResults<Member> objectResults;
private RealmChangeListener<RealmResults<Member>> objectTracker = (results) -> {
if(member != null && member.isValid()) {
return;
}
if(realm.isInTransaction()) {
handler.post(() -> {
setupMember(results);
});
} else {
setupMember(results);
}
};
private void setupMember(RealmResults<Member> members) {
if(member != null && member.isValid()) {
member.removeAllChangeListeners();
}
member = members.first(null);
if(member != null) {
member.addChangeListener((member, fieldChangeSet) -> {
// do whatever
});
}
}
private Member member;
private Realm realm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
realm = Realm.getDefaultInstance();
objectResults = realm.where(Member.class).equalTo("name", name).findAllAsync();
objectResults.addChangeListener(objectTracker);
}
@Override
protected void onDestroy() {
super.onDestroy();
if(member != null) {
member.removeAllChangeListeners();
}
objectResults.removeAllChangeListeners();
realm.close();
}
<小时/>
编辑:如果您不需要字段级更改:
private RealmResults<Member> objectResults;
private RealmChangeListener<RealmResults<Member>> objectTracker = (results) -> {
setupMember(results);
};
private void setupMember(RealmResults<Member> members) {
Member member = members.first(null);
if(member != null) {
updateViews(member); // update views with member
} else {
// member doesn't exist or was deleted
}
}
private Realm realm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
realm = Realm.getDefaultInstance();
objectResults = realm.where(Member.class).equalTo("name", name).findAllAsync();
objectResults.addChangeListener(objectTracker);
}
@Override
protected void onDestroy() {
super.onDestroy();
objectResults.removeAllChangeListeners();
realm.close();
}
关于java - 将 RealmObjectChangeListener 添加到异步下载的 RealmObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49385023/
假设异步API调用返回Member Data,它是一个RealmObject,并且Member被保存到realm中。 public class Member extends RealmObject {
我是一名优秀的程序员,十分优秀!