title: How to properly access Realm instance in a Service?
body: <p>I'm having trouble accessing a Realm instance inside a <code>Service</code> class which I'm using to handle a <code>Socket.IO</code> connection. I'm getting a <code>Realm</code> instance on the <code>onStartCommand</code> call like this.</p>
  
  <pre><code>public class SocketService extends Service{
      Realm realmInstance;
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          realmInstance = Realm.getDefaultInstance();
      }
  }
  </code></pre>
  
  <p>The problem is when I bind to a socket event, upon receiving that event I need to store some data into <code>Realm</code> but it throws an exception indicating the <code>Realm</code> object can't be accessed in a thread other than the one it was created on.</p>
  
  <pre><code>socket.on("register", new Emitter.Listener() {
              @Override
              public void call(Object... args) {
                  List&lt;MyRealmObject&gt; list = realmInstance.where(MyRealmObject.class).findAll();
              }
          });
  </code></pre>
  
  <blockquote>
    <p>java.lang.IllegalStateException: Realm access from incorrect thread.
    Realm objects can only be accessed on the thread they were created</p>
  </blockquote>
  
  <p>I event tried creating a separate instance for my event calls which I instantiated inside <code>CONNECT</code> call of socket.io to no avail.</p>