title: Android: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created
body: <p>So inside a <code>IntentService</code>, the app maybe active or inactive , <code>onHandleIntent</code> gets called , where I have placed this below code.This is where I store the data into realm.</p>
  
  <pre><code> Realm realm = null;
      try { 
          realm = Realm.getDefaultInstance();
          realm.executeTransactionAsync(new Realm.Transaction() {
              @Override
              public void execute(Realm realm) {
  
                  for (AppItem item : items) {
                      AppItem item2 = realm.createObject(AppItem.class, UUID.randomUUID().toString());
                      item2.mName = item.mName;
                      item2.mCount = item.mCount;
                      item2.mUsageTime = item.mUsageTime;
                  }
              }
          });
      } finally {
          if (realm != null) {
              realm.close();
          }
      }
  </code></pre>
  
  <p>Then I am trying to access it in <code>onPostExecute</code> in <code>AsyncTask</code>, In <code>doInBackground</code>, I am getting the <code>RealmResults&lt;AppItem&gt;</code>, then storing it into <code>List &lt;AppItem&gt;</code> and sending it to <code>onPostExecute</code> where this code is placed. <code>appItems</code> is ReamObject here</p>
  
  <pre><code> Realm backgroundRealm = Realm.getDefaultInstance();
              backgroundRealm.executeTransactionAsync(new Realm.Transaction() {
                  @Override
                  public void execute(Realm realm) {
                      for (AppItem item : appItems) { 
   //getting error here   if (item.mUsageTime &lt;= 0) continue;
                          mTotal += item.mUsageTime;
                          item.mCanOpen = mPackageManager.getLaunchIntentForPackage(item.mPackageName) != null;
  
  
                 }
                }
              });
  </code></pre>
  
  <p>Both I have done using <code>executeTransactionAsync</code>, still I get the following error.</p>
  
  <pre><code>java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
  </code></pre>