lundi 21 novembre 2016

Android software design using Singletons per-object class in Realm?

I'm designing an android app in realm that graphically charts data, using a very straight forward object class representation:

public class Graph implements RealmModel {
  @PrimaryKey
  private String UUID;
  public RealmList<DataSet> dataSets;
}

@RealmClass
  public class DataSet implements RealmModel {
  @PrimaryKey
  private String UUID;
  public RealmList<DataPoint> dataPoints;
}

@RealmClass
  public class DataPoint implements RealmModel {
  private String UUID;
}

Graph.Contains<DataSet.Contains<DataPoints>>

I'd like to run realm in it's own thread, separate from the UI. I'm considering using the Singleton design pattern to manage all 3 levels of data. There could be 1 singleton-per-real-model, 3 singletons in total, or there can be one master singleton.

In both cases, the singletons will have the traditional list methods avail. to them from RealmModel,

    realm.executeTransaction(new Realm.Transaction(){
        @Override
        public void execute(Realm realm){
            realm.copyToRealm(graph);
        }
    });

along with other methods I create:

public void bulkAdd() {
        @Override
        public void execute(Realm realm){
            for(int i=0; i<4;i++){
                //doStuff();
...      });

I want to use the Factory Pattern to manage the details of configuring each of the RealmModels. But before I get building I want to ask, are 3 singletons better than 1 for data management? Why?

A side-note, non-priority is that I would also like to take advantage of async if possible, ie:

    private void asyncAddQuote() {
    AsyncTask<Void, Void, Void> remoteItem = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {...

But I don't know which overall design patterns are best for this application. I need to keep in mind that Realm transactions are limited to one thread. I need to drive UI elements like RecyclerView from the UI thread. I've built the Graph singleton to run Graph level recyclerview and I'm about to build the rest. I would appreciate your thoughts on the software architecture options, considering all of the above. Thanks!

Aucun commentaire:

Enregistrer un commentaire