title: How to correctly use Realm
body: <p>I was trying to avoid creating and managing Realm objects, in my android application, for every fragment. I am thinking ThreadLocalVariable might be a good start.</p>
  
  <pre><code>public class RealmInstanceGenerator extends ThreadLocal&lt;Realm&gt; {
  
      public Realm getRealmForMyThread(Context context) {
  
          if(get() == null &amp;&amp; context != null)
              super.set(Realm.getInstance(context));
  
          return get();
      }
  
      public void setRealmForCurrentThread(Context context) {
  
          if(context != null)
              super.set(Realm.getInstance(context));
      }
  
      @Override
      protected Realm initialValue() {
          return null;
      }
  
      @Override
      public void remove() {
          if(get() != null) get().close();
          super.remove();
      }
  }
  </code></pre>
  
  <p>I would just create a static final object of RealmInstanceGenerator in my utils singleton class and call setRealmForCurrentThread in my MainActivity. Then I will call remove when my activity dies. For any new thread a new Realm object will be automatically generated.
  Is it a good strategy ?</p>