Commit e4fddf8f authored by Yusuke Iwaki's avatar Yusuke Iwaki

refactor RealmHelper to use try-with-resource

parent 80872e33
...@@ -108,9 +108,11 @@ abstract class AbstractDDPDocEventSubscriber implements Registerable { ...@@ -108,9 +108,11 @@ abstract class AbstractDDPDocEventSubscriber implements Registerable {
throws JSONException { throws JSONException {
//executed in RealmTransaction //executed in RealmTransaction
JSONObject json = new JSONObject().put("_id", docEvent.docID); JSONObject json = new JSONObject().put("_id", docEvent.docID);
for (int i = 0; i < docEvent.cleared.length(); i++) { if (docEvent.cleared != null) {
String fieldToDelete = docEvent.cleared.getString(i); for (int i = 0; i < docEvent.cleared.length(); i++) {
json.put(fieldToDelete, JSONObject.NULL); String fieldToDelete = docEvent.cleared.getString(i);
json.put(fieldToDelete, JSONObject.NULL);
}
} }
mergeJson(json, docEvent.fields); mergeJson(json, docEvent.fields);
realm.createOrUpdateObjectFromJson(getModelClass(), customizeFieldJson(json)); realm.createOrUpdateObjectFromJson(getModelClass(), customizeFieldJson(json));
......
...@@ -11,6 +11,7 @@ import io.realm.RealmObject; ...@@ -11,6 +11,7 @@ import io.realm.RealmObject;
import io.realm.RealmResults; import io.realm.RealmResults;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.json.JSONException;
import timber.log.Timber; import timber.log.Timber;
public class RealmHelper { public class RealmHelper {
...@@ -34,12 +35,9 @@ public class RealmHelper { ...@@ -34,12 +35,9 @@ public class RealmHelper {
return Collections.emptyList(); return Collections.emptyList();
} }
Realm realm = instance(); try (Realm realm = instance()) {
List<E> list = realm.copyFromRealm(objects); return realm.copyFromRealm(objects);
if (!realm.isClosed()) {
realm.close();
} }
return list;
} }
public <E extends RealmObject> E copyFromRealm(E object) { public <E extends RealmObject> E copyFromRealm(E object) {
...@@ -47,56 +45,33 @@ public class RealmHelper { ...@@ -47,56 +45,33 @@ public class RealmHelper {
return null; return null;
} }
Realm realm = instance(); try (Realm realm = instance()) {
E element = realm.copyFromRealm(object); return realm.copyFromRealm(object);
if (!realm.isClosed()) {
realm.close();
} }
return element;
} }
public interface Transaction<T> { public interface Transaction<T> {
T execute(Realm realm) throws Exception; T execute(Realm realm) throws JSONException;
} }
public <T extends RealmObject> T executeTransactionForRead(Transaction<T> transaction) { public <T extends RealmObject> T executeTransactionForRead(Transaction<T> transaction) {
Realm realm = instance(); try (Realm realm = instance()) {
T object;
try {
T source = transaction.execute(realm); T source = transaction.execute(realm);
object = source != null ? realm.copyFromRealm(source) : null; return source != null ? realm.copyFromRealm(source) : null;
} catch (Exception exception) { } catch (Exception exception) {
Timber.w(exception); Timber.w(exception);
object = null; return null;
} finally {
if (!realm.isClosed()) {
realm.close();
}
} }
return object;
} }
public <T extends RealmObject> List<T> executeTransactionForReadResults( public <T extends RealmObject> List<T> executeTransactionForReadResults(
Transaction<RealmResults<T>> transaction) { Transaction<RealmResults<T>> transaction) {
Realm realm = instance(); try (Realm realm = instance()) {
return realm.copyFromRealm(transaction.execute(realm));
List<T> object;
try {
object = realm.copyFromRealm(transaction.execute(realm));
} catch (Exception exception) { } catch (Exception exception) {
Timber.w(exception); Timber.w(exception);
object = null; return Collections.emptyList();
} finally {
if (!realm.isClosed()) {
realm.close();
}
} }
return object;
} }
public Task<Void> executeTransaction(final RealmHelper.Transaction transaction) { public Task<Void> executeTransaction(final RealmHelper.Transaction transaction) {
...@@ -107,19 +82,19 @@ public class RealmHelper { ...@@ -107,19 +82,19 @@ public class RealmHelper {
private Task<Void> executeTransactionSync(final RealmHelper.Transaction transaction) { private Task<Void> executeTransactionSync(final RealmHelper.Transaction transaction) {
final TaskCompletionSource<Void> task = new TaskCompletionSource<>(); final TaskCompletionSource<Void> task = new TaskCompletionSource<>();
final Realm realm = instance(); try (Realm realm = instance()) {
realm.executeTransaction(new Realm.Transaction() { realm.executeTransaction(new Realm.Transaction() {
@Override public void execute(Realm realm) { @Override public void execute(Realm realm) {
try { try {
transaction.execute(realm); transaction.execute(realm);
task.setResult(null); } catch (JSONException exception) {
} catch (Exception exception) { throw new RuntimeException(exception);
task.setError(exception); }
} }
} });
}); task.setResult(null);
if (!realm.isClosed()) { } catch (Exception exception) {
realm.close(); task.setError(exception);
} }
return task.getTask(); return task.getTask();
...@@ -133,27 +108,22 @@ public class RealmHelper { ...@@ -133,27 +108,22 @@ public class RealmHelper {
@Override public void execute(Realm realm) { @Override public void execute(Realm realm) {
try { try {
transaction.execute(realm); transaction.execute(realm);
} catch (Exception exception) { } catch (JSONException exception) {
task.setError(exception); throw new RuntimeException(exception);
if (!realm.isClosed()) {
realm.close();
}
} }
} }
}, new Realm.Transaction.OnSuccess() { }, new Realm.Transaction.OnSuccess() {
@Override public void onSuccess() { @Override public void onSuccess() {
if (task.trySetResult(null)) { realm.close();
if (realm != null && !realm.isClosed()) { task.setResult(null);
realm.close();
}
}
} }
}, new Realm.Transaction.OnError() { }, new Realm.Transaction.OnError() {
@Override public void onError(Throwable error) { @Override public void onError(Throwable error) {
if (task.trySetError(new Exception(error))) { realm.close();
if (!realm.isClosed()) { if (error instanceof Exception) {
realm.close(); task.setError((Exception) error);
} } else {
task.setError(new Exception(error));
} }
} }
}); });
......
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