可观察的光标在方向改变时未关闭

时间:2017-03-27 09:53:59

标签: android sqlite rx-java rx-android sqlbrite

在严格模式下运行时,当手机的方向发生变化时,我的应用程序会因错误而崩溃

  

在附加的堆栈跟踪中获取资源但从未发布。   有关避免资源泄漏的信息,请参阅java.io.Closeable。   java.lang.Throwable:显式终止方法' close'没有叫

奇怪的是,如果我在调试模式下运行应用程序并在所有cursor.close()上放置断点,只是为了确保它实际被击中,所有断点都被击中,应用程序永远不会崩溃。

这是我的片段,它调用另一个包含所有SqlBrite查询和游标的类(为简洁起见,代码已缩短)。

public class MenuSummary extends Fragment{

private DbHelper dbHelper;
private Observable<?> income1, income2;
private Observable imin;
private Subscription s;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dbHelper = new DbHelper(getContext());
}

@Override
public void onPause(){
    super.onPause();
    s.unsubscribe();
}

@Override
public void onResume(){
    super.onResume();
    displaySummary(selectedYear,selectedMonth);
}

@Override
public void onStop() {
    super.onStop();
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
}

private void displaySummary(final int selectedYear, final int selectedMonth){

    income1 = dbHelper.getIncome(selectedYear,selectedMonth,1);
    income2 = dbHelper.getIncome(selectedYear,selectedMonth,2);

    List<Observable<?>> myObservables = Arrays.asList(income1,income2);
    imin = Observable.combineLatest(myObservables, new FuncN<List<BigDecimal>>() {
        @Override
        public List<BigDecimal> call(Object... args) {
            List<BigDecimal> listIncome = new ArrayList<~>();
            listIncome.add((BigDecimal) args[0]);
            listIncome.add((BigDecimal) args[1]);
            return listIncome ;
        }
    }
).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
s = imin.subscribe(new Action1<List<BigDecimal>>() {
@Override
public void call(List<BigDecimal> expensesAndBalance) {
    //do plenty of view.settext here
}});
}

以下是包含getIncome函数的DbHelper类如何:

任何人都可以查明我哪里出错了吗?

   public class DbHelper {

        private MySQLiteHelper mySQLiteHelper;
        SqlBrite sqlBrite = new SqlBrite.Builder().build();
        BriteDatabase briteDb;
        Subscription subscription;

        private static DbHelper instance;
        public DbHelper(Context context) {
            mySQLiteHelper = new MySQLiteHelper(context);
            sqlBrite = new SqlBrite.Builder().build();
            briteDb = sqlBrite.wrapDatabaseHelper(mySQLiteHelper,Schedulers.io());
        }

        public Observable<BigDecimal> getIncome(final int endYear, final int endMonth, final int moneyJar){
        final String[] args = new String[]{moneyJar + "", endYear + "", endMonth + "", endYear + "", };
        Observable<BigDecimal> myObservable;
        myObservable = briteDb.createQuery(MySQLiteHelper.TABLE_INCOME, "SELECT total FROM " + MySQLiteHelper.TABLE_INCOME +
                " WHERE moneyJar = ? AND ((year = ? AND month <= ?) OR (year < ?)) and isDeleted = 0", args)
                .map(new Func1<SqlBrite.Query, BigDecimal>() {
                    @Override
                    public BigDecimal call(SqlBrite.Query query) {
                        Cursor cursor = query.run();
                        BigDecimal income, incomeTotal = new BigDecimal(0);
                        if (cursor != null) {
                            try {
                                if (cursor.getCount() > 0 && cursor.moveToFirst()) {
                                    do {
                                        income = new BigDecimal(cursor.getString(cursor.getColumnIndex("total")));
                                        incomeTotal = incomeTotal.add(income);
                                    } while (cursor.moveToNext());
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                cursor.close();
                            }
                        }
                        return incomeTotal;
                    }
                });
        return myObservable;
    }
}

1 个答案:

答案 0 :(得分:1)

您没有关闭BriteDatabase个实例。

您应该在片段关闭时执行此操作:

briteDb.close();

来自docs

  

关闭基础SQLiteOpenHelper并删除缓存的可读写数据库。这并不妨碍现有的observable保留现有的引用,也不会试图为新的订阅创建新的引用。