What is the best practice for managing an instance of an area in clean architecture?

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); } }); } 
+5
source share
2 answers

If you want to make a clear separation between the user interface and database layers in your code and want to abstract from your database logic so that, ideally, your activity could call the database level without knowing how this level is implemented, then Realm probably won't is what you are looking for.

Realm objects are attached to realm instances, which means that if you retrieve an object from a realm instance and then close that instance (which you should), you can no longer use the object. This defeats the whole purpose of using Realm.

If you intend to use Realm, you must maintain the logic of the sphere closely related to your actions / services, etc., and do not try to hide it at a separate level so that you have full control over it.


  .map(new Func1<RealmObject, T>() { @Override public T call(RealmObject realmObject) { Object o = createObjectFromRealm(realmObject); realm.close(); return o; } }); 
+4
source

One of the main aspects of clean architecture is the allocation of core libraries (i.e. Realm). Since Realm, RealmObject, RealmResults are not accessible outside the stream in which they are created, it makes it even more important to keep isolated calculations in Realm and Realm from the rest of the code.

You are using RxJava in the queryAsync () method, and at the same time you are using the executeTransactionAsync () method, which challenges the whole purpose of using RxJava. You could do it

 public void insert(T object){ final Realm realm = RealmProvider.getRealm(); realm.executeTransaction(realm1 -> realm1.copyToRealmOrUpdate(createRealmObject(object))); realm.close(); } 

In a good architecture, for each jsonModel class, there must be a corresponding realmModel class and a DAO (data access object). The DAO class should accept jsonModel as an argument and should return jsonModel as the result. All operations related to Realm should be restricted in the DAO file, so none of the code other than DAO and realmModel knows about Realm.

Here is an article about Realm best practices with good architecture https://medium.com/@Viraj.Tank/realm-integration-in-android-best-practices-449919d25f2f

An example project is also shown demonstrating the integration of Realm on Android with MVP (Presentation Viewer), RxJava, Retrofit, Dagger, Annotations and Testing. https://github.com/viraj49/Realm_android-injection-rx-test

0
source

All Articles