My project uses clean architecture. In this situation, the user interface layer is separated from the domain level. Therefore, I think it would be better if the user interface layer did not have a realm instance. Typically, doc recommends managing a realm instance in the Activity lifecycle, how should I handle an instance of a region then?
To be more clear, my project is too heavy to modify all extends RealmObject . Therefore, I use a separate object for persistent data. When the api call completes, the business object is converted to a scope object, in contrast, when a request is from a scope. I create a method as follows:
public void insert(T object){ final Realm realm = RealmProvider.getRealm(); realm.executeTransactionAsync(new Realm.Transaction() { @Override public void execute(Realm realm) { realm.copyToRealmOrUpdate(createRealmObject(object)); } }, new Realm.Transaction.OnSuccess() { @Override public void onSuccess() { realm.close(); } }, new Realm.Transaction.OnError() { @Override public void onError(Throwable error) { realm.close(); } }); }
Actually, it works fine. But below I do not know how to handle the closing instance of the area.
public Observable<T> queryAsync(Condition<? extends RealmObject> condition) { final Realm realm = RealmProvider.getRealm(); return condition.getQuery(realm).findFirstAsync() .asObservable() .filter(new Func1<RealmObject, Boolean>() { @Override public Boolean call(RealmObject realmObject) { return realmObject.isLoaded(); } }) .map(new Func1<RealmObject, T>() { @Override public T call(RealmObject realmObject) { return createObjectFromRealm(realmObject); } }); }
source share