title: Android: Realm.getInstance(context) returns an already closed realm instance
body: <p><code>Realm.getInstance(context)</code> will rarely return an already closed realm instance. How is this possible?</p>
  
  <p>I am using Realm with RxJava, per <a href="https://realm.io/news/using-realm-with-rxjava/" rel="nofollow">https://realm.io/news/using-realm-with-rxjava/</a></p>
  
  <p>In particular, this method throws an <code>IllegalStateException: This Realm instance has already been closed, making it unusable.</code></p>
  
  <pre><code>@Override
      public void call(final Subscriber&lt;? super RealmList&lt;T&gt;&gt; subscriber) {
          final Realm realm = Realm.getInstance(context);
          subscriber.add(Subscriptions.create(new Action0() {
              @Override
              public void call() {
                  try {
                      realm.close();
                  } catch (RealmException ex) {
                      subscriber.onError(ex);
                  }
              }
          }));
  
          RealmList&lt;T&gt; object;
          realm.beginTransaction(); //THROWS EXCEPTION
  
          //...
  }
  </code></pre>
  
  <p>If I comment out the <code>realm.close();</code> issue, no problems. Though I think this will lead into a native memory leak, then.</p>
  
  <p>My best guess as to why this is occurring is that multiple calls to this method are being made, and if these method calls line up perfectly, then an already closed realm instance can be retrieved?</p>
  
  <p>EDIT: By using <code>Schedulers.io()</code>, I get a lot of <code>Calling close() on a Realm that is already closed</code> warnings. My guess here is that somehow after I am done using the <code>.io()</code> thread, the realm instance is automatically closed. Not sure why this would happen though.</p>
  
  <p>EDIT2: By switching to using <code>Schedulers.newThread()</code> instead of <code>Schedulers.io()</code> for my observables, the issue stopped appearing. But I am seeing a lot of <code>Remember to call close() on all Realm instances</code> warnings. I am pretty sure I am closing them, so I am very confused about this.</p>
  
  <p>EDIT3: By switching to using <code>AndroidSchedulers.mainThread()</code>, no errors. Except my Realm calls run on the main thread, which is bad bad bad. My guess why this causes no warnings is because the <code>realm</code> now lives on the main thread, which is also where <code>realm.close()</code> is called (via the <code>rx.subscriber</code>). </p>
  
  <p>EDIT4: Here's the logic for my realm observable call.</p>
  
  <pre><code>@Override
  public Observable&lt;List&lt;ImageArticleCategoryEntity&gt;&gt; getArticleBuckets() {
  
      return RealmObservable.list(context, GET_ARTICLE_BUCKETS)
              .filter(FILTER_OUT_NULL_OR_EMPTY_LIST)
              .switchIfEmpty(refreshAndSaveAndLoadNewDataFromDb)
              .map(CONVERT_FROM_REALMLIST_TO_IMAGE_ARTICLE_ENTITYLIST);
  
  }
  
  public void loadArticleImages() {
      articleRepo.getArticleBuckets()
              .subscribeOn(RealmThread.get())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(new Subscriber&lt;List&lt;ImageArticleCategoryEntity&gt;&gt;() {
                  @Override
                  public void onCompleted() {
                      Timber.v("Loading article images complete!");
                      if (view != null)
                          view.hideLoadingAnimation();
                  }
  
                  @Override
                  public void onError(Throwable e) {
                      Timber.e("Error loading article images", e);
                      Log.e("tag", "Error loading article images", e);
                      if (view != null) {
                          view.hideLoadingAnimation();
                          view.showLoadingErrorToast();
                      }
                  }
  
                  @Override
                  public void onNext(List&lt;ImageArticleCategoryEntity&gt; integerImageArticleCategoryEntityHashMap) {
                      if (view != null)
                          view.loadArticleImages(integerImageArticleCategoryEntityHashMap);
                  }
              });
  </code></pre>