2016-11-13 5 views
0

以下のコードブロックを使用してCustomerオブジェクトをRealmに追加すると、オブジェクトが正常に追加されます。しかし、アイテムの挿入が完了したときや失敗したときにToastメッセージ経由でフィードバックを提供しようとしています。アクティビティ/フラグメント外のレルムを正しく閉じる方法

@Override 
public void addCustomer(final Customer customer) { 
    Realm insertRealm = Realm.getDefaultInstance(); 
    insertRealm.executeTransactionAsync(new Realm.Transaction() { 
     @Override 
     public void execute(Realm backgroundRealm) { 
      long id = customerPrimaryKey.incrementAndGet(); 
      customer.setId(id); 
      backgroundRealm.copyToRealm(customer); 
     } 
    }, new Realm.Transaction.OnSuccess() { 
     @Override 
     public void onSuccess() { 
      Log.d(TAG, "Customer Added"); 
     } 
    }, new Realm.Transaction.OnError() { 
     @Override 
     public void onError(Throwable error) { 
      Log.d(TAG, error.getMessage()); 
     } 
    }); 
    insertRealm.close(); 

} 

上記のコードブロックが正常にオブジェクトを挿入し、私はそれを開始するために使用されたレルムを閉じているので、しかし、成功とエラーコールバックは達していません。レルムを閉じなければ、onErrorとonSuccessのコールバックに達しますが、レルムがリークするため、アプリケーションがクラッシュします。

このようなコールバック内でレルムを閉じるコードを更新しようとしましたが、クラッシュしました。

私の質問は、アクティビティ/フラグメントライフサイクルの外でレルムを閉じる方法があるかどうかです。レルムのグローバルシングルトンを保持し、それを使用するクラスに注入できますか?更新

- を追加しましたStackTrack

するonSuccessとのonErrorコールバックの作品にレルムを閉じる
11-13 06:59:07.545 2249-2257/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/metrics.db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed. 
11-13 06:59:07.549 2249-2257/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/help_responses.db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed. 
11-13 06:59:07.557 2249-2257/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/auto_complete_suggestions.db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed. 
+0

クラッシュスタックトレース – EpicPandaForce

+0

である私は、スタックトレースを追加して、私がレルムを使用していたときに、それはSQLiteのよう漏出したデータベースが報告されていることが面白いです。私はtry/catchでrealm.closeステートメントをラップしていますが、今はatleastアプリケーションがクラッシュしていません。 –

+0

これらのリークはGoogle GMSに属しているため、領域とは関係ないため、https://www.android.com/gms/と思っています – EpicPandaForce

答えて

0

は、私はと格闘して問題が漏れている既存のSQLiteOpenHelperクラスです。 SQLiteへの参照からプロジェクトをクリーンアップし、すべて正常に動作しています。もう一度このコードブロックが動作します。

}, new Realm.Transaction.OnSuccess() { 
     @Override 
     public void onSuccess() { 
      Log.d(TAG, "Customer Added"); 
      insertRealm.close(); 
     } 
    }, new Realm.Transaction.OnError() { 
     @Override 
     public void onError(Throwable error) { 
      Log.d(TAG, error.getMessage()); 
      insertRealm.close(); 
     } 
    }); 
関連する問題