title: Realm.io and asynchronous queries
body: <p>We are starting to use Realm.io in an android app project. But something that we don't like with the Realm.io API is the absence of any asynchronous way to query the database. In old projects we used DbQuery and Sqlite, so we are used to make the database queries inside threads or <code>AsyncTask</code>'s. But is quite disturbing see that in all the examples the queries are made in the <code>UiThread</code>. Isn't this a bad thing for the app performance?. We tried to make the queries inside threads or AsyncTasks but we get an error when we access our model objects back in the UiThread, saying that we cannot access RealObjects in threads that wasnt the ones where they was queried. Here is our example code:</p>
  
  <pre><code>service.getDatabaseHelper()
              .with(getActivity()) //Context that I use to instance the Realm instance
              .onFinishQuery( new DatabaseHelper.IQueryGetCallback&lt;UserRealm&gt;() {
                  @Override
                  public void onGetResult(UserRealm[] results) {
                      for(UserRealm aUser : results){
                          //Here is where it crashes
                          Log.d("Log","Username -&gt; "+aUser.getName());
                      }
                  }
              })
              .getUsersFromDb();
  //.......
  
  //In our "DAO" class
  public void getUsersFromDb(){
      new GetQueryTask() {
          @Override
          public UserRealm[] onQueryReadyToBeExecuted(Realm realmInstance) {
              RealmQuery&lt;UserRealm&gt; query = realmInstance.where(UserRealm.class);
              RealmResults&lt;UserRealm&gt; result = query.findAll();
              // TODO hacer que devuelva un array
  
              ArrayList&lt;UserRealm&gt; users = new ArrayList&lt;UserRealm&gt;();
              for (UserRealm u : result) {
                  //Here we can read the RealObject's fine
                  users.add(u);
              }
              return users.toArray(new UserRealm[users.size()]);
          }
      }.execute();
  }
  
  //Our abstract task that wraps all the instantiation-transaction behaviour
  public abstract class GetQueryTask&lt;T extends RealmObject&gt; extends AsyncTask&lt;Void, Void, T[]&gt; {
      @Override
      protected T[] doInBackground(Void... params) {
          //We tried to instantiate this class in several places, here
          //Send it as parameter through the AsyncTask
          //context is set in the with(Context ctx) method.
          Realm realm = Realm.getInstance(context);
          return onQueryReadyToBeExecuted(realm);
      }
  
      public abstract T[] onQueryReadyToBeExecuted(Realm realmInstance);
  
      @Override
      protected void onPostExecute(T[] result) {
          mCallback.onExecute(result);
      }
  }
  </code></pre>
  
  <p>Our major concern isn't is the "bug" of this code, but if it's okay to do the queries in the UiThread as the Realm.io developers do in their examples. If is that the case, all this code to perform the queries asynchronously will not be neccesary any more.</p>