title: Have I used Singleton with Realm Database correct?
body: <p>I have an app where the user can create/delete/edit to a list from a DialogFragment. Before I had a method like this in my DialogFragments: <code>MainActivtity.adapter.add(String name, String location, double price);</code></p>
  
  <p><code>adapter</code> is my adapter object for the recyclerView. The class for the adapter had my create/delete/edit methods for the items in the recyclerView. Which was called like shown above which also is a horrible way to call mehtods as I understand.</p>
  
  <p>So I choose to put all these CRUD methods in a one singleton class and call these method like this: <code>Service.getInstance().add(...);</code></p>
  
  <p>Is this a correct approach and what could I have done better? </p>
  
  <p>This is how I made the singleton class that now contains my CRUD methods, instead of putting them in my adapter class for the recyclerView as before.</p>
  
  <pre><code>public class Service {
  
  private static Realm realm;
  private static Service service = new Service();
  
  private Service() {
      realm = Realm.getInstance(App.getAppContex());
  }
  
  public static Service getInstance(){
  
      if(service == null){
          service = new Service();
      }
      return service;
  }
  
  
  
     public void add(String name, String location, double price) {
  
  
      ShopListItem shopListItem = new ShopListItem();
  
      shopListItem.setName(name);
      shopListItem.setLocation(location);
      shopListItem.setPrice(price);
      shopListItem.setTimestamp(System.currentTimeMillis());
      shopListItem.setIsBought(0);
  
      realm.beginTransaction();
      realm.copyToRealm(shopListItem);
      realm.commitTransaction();
  }
  
  
  public void removeItem(int position, List&lt;ShopListItem&gt; shopListItems) {
  
      realm.beginTransaction();
      shopListItems.remove(position);
      realm.commitTransaction();
  }
  </code></pre>
  
  <p><strong>This class is just used for getting the global/Application context</strong></p>
  
  <pre><code>public class App extends Application {
  
  
  public static Application sApplication;
  
  public static Application getsApplication(){
      return sApplication;
  }
  
  public static Context getAppContex(){
  
      return getsApplication();
  }
  
  @Override
  public void onCreate() {
      super.onCreate();
      sApplication = this;
  }
  }
  </code></pre>
  
  <p><strong><em>Question updated from here:</em></strong></p>
  
  <p><strong>Here is the new approach based on suggestions from comments:</strong>
  Now everytime I want to make a CRUD operation in the Realm Database, I always start with the getDefaultInstance for my realm object and finish of with realm.close(); this procces is reapted in every CRUD method. </p>
  
  <pre><code>public class Service {
  
  private Realm realm;
  private static Service service = new Service();
  
  private Service() {
  
  
  }
  
  public static Service getInstance(){
  
      if(service == null){
          service = new Service();
      }
      return service;
  }
  
  public void removeItem(int position, List&lt;ShopListItem&gt; shopListItems) {
      //new: realm = Realm.getDefaultInstance();
  
      realm = Realm.getDefaultInstance();
      realm.beginTransaction();
      shopListItems.remove(position);
      realm.commitTransaction();
      realm.close();
      //new: realm.close();
  }
  </code></pre>
  
  <p><strong>Realm configurations is now moved to my Application class as suggested by Realm.</strong></p>
  
  <pre><code>public class App extends Application {
  
  
  public static Application sApplication;
  
  public static Application getsApplication(){
      return sApplication;
  }
  
  public static Context getAppContex(){
  
      return getsApplication();
  }
  
  @Override
  public void onCreate() {
      super.onCreate();
      sApplication = this;
  final RealmConfiguration realmConfiguration = new    RealmConfiguration.Builder(App.getAppContex()).build();
      realm.setDefaultConfiguration(realmConfiguration);
  }
  }
  </code></pre>