2016-07-19 49 views
4

我想在執行完成後關閉我的領域實例executeTransactionAsync。原因是我的應用程序主線程凍結,我認爲它的原因是後臺領域實例在執行完成後沒有被關閉。看到我的代碼如下:執行完畢後關閉領域實例

realm.executeTransactionAsync(new Realm.Transaction() { 
        @Override 
        public void execute(Realm realm) { 
         // Execute realm code 
         realm.copyToRealmOrUpdate(myData); 
         // Can I close the realm instance here without getting an 
         // error? realm.close(); causes an error. 
        } 
       }, new Realm.Transaction.OnSuccess() { 
        @Override 
        public void onSuccess() { 
         Log.i("CB", "success"); 
         // looks like I cannot access the execute Realm 
         // instance here. 
         // Closing realm.getDefaultInstance does not change my issue 
        } 
       }, new Realm.Transaction.OnError() { 
        @Override 
        public void onError(Throwable error) { 
         Log.i("CB", "error - " + error.getMessage()); 
        } 
       }); 
      } 

請參閱我的意見。我的應用程序屏幕變成黑色。執行成功完成並調用onSuccess(),但我無法訪問execute領域實例從此處關閉它。

對於我可以嘗試的方法,您有什麼建議嗎?難道我做錯了什麼?

預先感謝您。

編輯

07-19 11:43:42.379 8146-8146/com.shortterminsurance.shortterm I/CB: success 
07-19 11:43:43.258 8146-8152/com.shortterminsurance.shortterm W/art: Suspending all threads took: 33.234ms 
07-19 11:43:43.266 8146-8156/com.shortterminsurance.shortterm I/art: Background partial concurrent mark sweep GC freed 476307(17MB) AllocSpace objects, 512(10MB) LOS objects, 40% free, 33MB/55MB, paused 7.261ms total 163.497ms 
07-19 11:43:44.131 8146-8156/com.shortterminsurance.shortterm I/art: Background sticky concurrent mark sweep GC freed 408160(9MB) AllocSpace objects, 459(15MB) LOS objects, 35% free, 35MB/55MB, paused 10.287ms total 147.823ms 
07-19 11:43:44.834 8146-8152/com.shortterminsurance.shortterm W/art: Suspending all threads took: 103.676ms 
07-19 11:43:44.848 8146-8156/com.shortterminsurance.shortterm W/art: Suspending all threads took: 13.424ms 

這是我的logcat我的onSuccess被調用後。我認爲在execute的領域的背景實例保持運行的原因:(。

+0

等待您要關閉哪個領域實例?你用'background instance'指的是什麼? –

+0

@TimCastelijns我想在'execute'中關閉領域實例。將更快地編輯我的問題 – Lunchbox

+0

你的意思是這裏傳遞的實例public void execute(Realm realm){'? –

回答

11

是這裏

@Override 
public void execute(Realm realm) { 

通過關閉你的領域實例。你不必擔心自己關閉它。

如果您檢查Realm.executeTransactionAsync()的源代碼,你可以找到

bgRealm.beginTransaction(); 
try { 
    transaction.execute(bgRealm); 

    if (!Thread.currentThread().isInterrupted()) { 
     bgRealm.commitTransaction(false, new Runnable() { 
      @Override 
      public void run() { 
       // The bgRealm needs to be closed before post event to caller's handler to avoid 
       // concurrency problem. eg.: User wants to delete Realm in the callbacks. 
       // This will close Realm before sending REALM_CHANGED. 
       bgRealm.close(); 
      } 
     }); 
     transactionCommitted = true; 
    } 
} 
... 

所以首先它自己的交易要求.execute(),之後,它關閉了它傳遞給你的領域實例。

1

正如Tim Castelijns所說,executeTransactionAsync已經關閉領域,反正,不關閉領域的實例不會導致UI塊,但拋出異常如果您嘗試打開一個領域實例,所以你應該有另一個問題

+0

如果可以的話,我會。查看我添加的註釋,我無法訪問onSuccess/onError中的背景領域實例。 – Lunchbox

+0

Tim Castelijns說executeTransactionAsync()已經關閉領域,所以你應該有另一個問題 – MikeOx

2

這是一個可能性:

  final Realm realm = Realm.getDefaultInstance(); 
      realm.executeTransactionAsync(new Realm.Transaction() { 
       @Override 
       public void execute(Realm realm) { 
        // Execute realm code 
        realm.copyToRealmOrUpdate(myData); 
       } 
      }, new Realm.Transaction.OnSuccess() { 
       @Override 
       public void onSuccess() { 
        Log.i("CB", "success"); 
        realm.close(); 
       } 
      }, new Realm.Transaction.OnError() { 
       @Override 
       public void onError(Throwable error) { 
        Log.i("CB", "error - " + error.getMessage()); 
        realm.close(); 
       } 
      }); 
     } 

雖然背景境界情況下自動關閉