Commit 2aeaa74a authored by Yusuke Iwaki's avatar Yusuke Iwaki

improve "Stops the same object from been propagated over and over as if updated"

parent 32229162
...@@ -20,10 +20,6 @@ public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmRes ...@@ -20,10 +20,6 @@ public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmRes
protected T extractObjectFromResults(RealmResults<T> results) { protected T extractObjectFromResults(RealmResults<T> results) {
return results.isEmpty() ? null : results.last(); return results.isEmpty() ? null : results.last();
} }
protected boolean isSame(T element1, T element2) {
return element1.equals(element2);
}
} }
private final Query<T> query; private final Query<T> query;
...@@ -45,23 +41,21 @@ public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmRes ...@@ -45,23 +41,21 @@ public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmRes
return this; return this;
} }
private T previousResult; private String previousResultString;
@Override @Override protected final RealmResults<T> queryItems(Realm realm) {
protected final RealmResults<T> queryItems(Realm realm) {
return query.query(realm).findAll(); return query.query(realm).findAll();
} }
@Override @Override protected final RealmChangeListener<RealmResults<T>> getListener() {
protected final RealmChangeListener<RealmResults<T>> getListener() {
return new RealmChangeListener<RealmResults<T>>() { return new RealmChangeListener<RealmResults<T>>() {
@Override @Override public void onChange(RealmResults<T> element) {
public void onChange(RealmResults<T> element) {
T currentResult = impl.extractObjectFromResults(element); T currentResult = impl.extractObjectFromResults(element);
if (previousResult != null && impl.isSame(previousResult, currentResult)) { String currentResultString = currentResult != null ? currentResult.toString() : null;
if (previousResultString != null && previousResultString.equals(currentResultString)) {
return; return;
} }
previousResult = currentResult; previousResultString = currentResultString;
if (onUpdateListener != null) { if (onUpdateListener != null) {
onUpdateListener.onUpdateObject( onUpdateListener.onUpdateObject(
currentResult != null ? realm.copyFromRealm(currentResult) : null); currentResult != null ? realm.copyFromRealm(currentResult) : null);
...@@ -70,8 +64,9 @@ public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmRes ...@@ -70,8 +64,9 @@ public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmRes
}; };
} }
public void sub() { public void sub() {
previousResult = null; previousResultString = null;
super.sub(); super.sub();
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment