Android adapter dataset instantiation practices with Realm -


i wondering matter instantiate adapters dataset realm? fetch data adapter needs in adapters constructor , instantiate dataset there, examples i've seen fetch data beforehand in activity creating adapter , pass adapter parameter.

with sqlite seems more arbitrary, since i'm using realm need open realm connection every time want access database , keep data available need keep connection open. keeping connection open in activity seems unnecessary since might need make queries in adapter having open connection realm within adapter anyways.

is there higher reason fetch dataset beforehand or matter of preference?

since i'm using realm need open realm connection every time want access database

wrong, need 1 open instance given thread in order access database.

keeping connection open in activity "seems unnecessary" since might need make queries in adapter

in case can have activity-level realm instance "scoped dependency", can share through context via getsystemservice() if that's do.

public class myactivity extends activity {     realm realm;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         realm = realm.getdefaultinstance();         //...     }      @override     public void ondestroy() {         if(realm != null) {            realm.close();         }         super.ondestroy();     }     ...      @override     public object getsystemservice(string key) {         if("realm".equals(key)) {             return realm;         }         return super.getsystemservice(key);     } }  public class myadapter extends recyclerview.adapter<mymodelviewholder> {     private final context context;     realm realm;      realmresults<mymodel> results;     private final realmchangelistener listener = new realmchangelistener() {         @override         public void onchange(object element) {             notifydatasetchanged();         }     }      public myadapter(context context) {         this.context = context;         //noinspection resourcetype         realm = (realm)context.getsystemservice("realm");         results = realm.where(mymodel.class).findall();         results.addchangelistener(listener);     }      ... } 

thus having open connection realm within adapter anyways.

wrong

is there higher reason fetch dataset beforehand or matter of preference?

it's because adapter, supposed describe how show elements of dataset, become god determines data must show.

although fair, it's harder externally manage data-set; must hold strong reference result set anyways. when don't bother unit-testability, obtain results inside adapter itself. work.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -