Commit 5f6b098d authored by Yusuke Iwaki's avatar Yusuke Iwaki

define RealmHelper#isObjectExists

parent 56692224
...@@ -7,6 +7,7 @@ import android.support.v7.widget.RecyclerView; ...@@ -7,6 +7,7 @@ import android.support.v7.widget.RecyclerView;
import io.realm.Realm; import io.realm.Realm;
import io.realm.RealmConfiguration; import io.realm.RealmConfiguration;
import io.realm.RealmObject; import io.realm.RealmObject;
import io.realm.RealmQuery;
import io.realm.RealmResults; import io.realm.RealmResults;
import org.json.JSONException; import org.json.JSONException;
...@@ -73,6 +74,19 @@ public class RealmHelper { ...@@ -73,6 +74,19 @@ public class RealmHelper {
} }
} }
public interface Query<T extends RealmObject> {
RealmQuery<T> query(Realm realm);
}
public <T extends RealmObject> boolean isObjectExists(Query<T> query) {
try (Realm realm = instance()) {
return query.query(realm).count() > 0;
} catch (Exception exception) {
RCLog.w(exception);
return false;
}
}
private boolean shouldUseSync() { private boolean shouldUseSync() {
// ref: realm-java:realm/realm-library/src/main/java/io/realm/AndroidNotifier.java // ref: realm-java:realm/realm-library/src/main/java/io/realm/AndroidNotifier.java
// #isAutoRefreshAvailable() // #isAutoRefreshAvailable()
...@@ -140,7 +154,7 @@ public class RealmHelper { ...@@ -140,7 +154,7 @@ public class RealmHelper {
} }
public <T extends RealmObject> RealmObjectObserver<T> createObjectObserver( public <T extends RealmObject> RealmObjectObserver<T> createObjectObserver(
RealmObjectObserver.Query<T> query) { RealmHelper.Query<T> query) {
return new RealmObjectObserver<T>(this, query); return new RealmObjectObserver<T>(this, query);
} }
......
...@@ -3,17 +3,16 @@ package chat.rocket.android.realm_helper; ...@@ -3,17 +3,16 @@ package chat.rocket.android.realm_helper;
import io.realm.Realm; import io.realm.Realm;
import io.realm.RealmChangeListener; import io.realm.RealmChangeListener;
import io.realm.RealmObject; import io.realm.RealmObject;
import io.realm.RealmQuery;
import io.realm.RealmResults; import io.realm.RealmResults;
public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmResultsObserver<T> { public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmResultsObserver<T> {
private final Query<T> query; private final RealmHelper.Query<T> query;
private OnUpdateListener<T> onUpdateListener; private OnUpdateListener<T> onUpdateListener;
private Impl<T> impl; private Impl<T> impl;
private String previousResultString; private String previousResultString;
/*package*/ RealmObjectObserver(RealmHelper helper, Query<T> query) { /*package*/ RealmObjectObserver(RealmHelper helper, RealmHelper.Query<T> query) {
super(helper); super(helper);
this.query = query; this.query = query;
setImpl(new Impl<T>()); setImpl(new Impl<T>());
...@@ -54,10 +53,6 @@ public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmRes ...@@ -54,10 +53,6 @@ public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmRes
super.sub(); super.sub();
} }
public interface Query<T extends RealmObject> {
RealmQuery<T> query(Realm realm);
}
public interface OnUpdateListener<T extends RealmObject> { public interface OnUpdateListener<T extends RealmObject> {
void onUpdateObject(T element); void onUpdateObject(T element);
} }
......
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