0

我的 SQLiteDatabase 给我带来了一些麻烦。当用户关闭应用程序时,在 onPause() 中,AsyncTask 开始保存数据。我不知道这是否是最好的方法,但我这样做的方法是,首先数据库删除所有现有数据(不是太多,两个表之间可能有20-30个值),然后将新数据添加回数据库。

deleteAll() 方法工作得很好,但是当它尝试添加每个项目时,它在第 6 个之后失败。

这是我的 AsyncTask 的 doInBackground() 方法:

@Override
    protected Void doInBackground(Void... args) 
    {           
        //open database
        try 
        {
            myDbHelper.openDatabase();
        }catch(SQLException sqle)
        {    
            throw sqle;  
        }

        myDbHelper.deleteAll();

        //add all ingredients & units
        for (int i = 0; i < ingredients.size(); i++)
            myDbHelper.addIngredient(ingredients.get(i));
        for (int i = 0; i < units.size(); i++)
            myDbHelper.addUnit(units.get(i));

        myDbHelper.close();


        return null;
    }

接下来,这里是我的 DatabaseHelper 类的 deleteAll()、addIngredient() 和 addUnit() 方法:

public void deleteAll() 
{
    int deletedIngredientCount = myDataBase.delete(TABLE_INGREDIENTS, "1", null);
    Log.d("QBCPro", deletedIngredientCount + " ingredients deleted successfully");

    int deletedUnitCount = myDataBase.delete(TABLE_UNITS, "1", null);
    Log.d("QBCPro", deletedUnitCount + " units deleted successfully");
}

public void addIngredient(Ingredient ingredient) 
{
    ContentValues values = new ContentValues();
    values.put(ING_KEY_NAME, ingredient.getName());
    values.put(ING_KEY_DENSITY, ingredient.getDensity());

    // Insert Row
    int row = (int) myDataBase.insert(TABLE_INGREDIENTS, null, values);
    if (row == -1)
        Log.d("QBCPro", "An error occurred, row == " + row);
    else
        Log.d("QBCPro", "Successfully inserted " + values.get(ING_KEY_NAME) + " into row " + row);
}

public void addUnit(Unit unit)
{
    ContentValues values = new ContentValues();
    values.put(UNIT_KEY_NAME, unit.getName());
    values.put(UNIT_KEY_BASE_VALUE, unit.getBaseValue());
    values.put(UNIT_KEY_FINAL_VALUE, unit.getFinalValue());
    values.put(UNIT_KEY_IS_WEIGHT, unit.getIsWeight());

    // Insert Row
    myDataBase.insert(TABLE_UNITS, null, values);
}

最后,这是日志。如您所见,它突然结束,只调用了 addIngredient() 六次。还有 showStatusIcon 警告,但我不知道它可能来自哪里,或者是什么原因造成的。

02-13 18:47:32.320: D/QBCPro:::(25264): SaveAllTask: Saving to database...
02-13 18:47:32.405: W/IInputConnectionWrapper(25264): showStatusIcon on inactive InputConnection
02-13 18:47:32.445: D/QBCPro(25264): DatabaseHelper deleteAll() called.
02-13 18:47:32.480: D/QBCPro(25264): 11 ingredients deleted successfully
02-13 18:47:32.510: D/QBCPro(25264): 12 units deleted successfully
02-13 18:47:32.510: D/QBCPro(25264): Leaving DatabaseHelper deleteAll() method.
02-13 18:47:32.540: D/QBCPro(25264): Successfully inserted almonds (ground) into row 1
02-13 18:47:32.575: D/QBCPro(25264): Successfully inserted baking powder into row 2
02-13 18:47:32.610: D/QBCPro(25264): Successfully inserted baking soda into row 3
02-13 18:47:32.640: D/QBCPro(25264): Successfully inserted butter into row 4
02-13 18:47:32.685: D/QBCPro(25264): Successfully inserted cocoa powder into row 5
02-13 18:47:32.720: D/QBCPro(25264): Successfully inserted flour (all-purp) into row 6

我猜这会导致内存泄漏或其他原因,因为这是日志的结尾。它还会导致应用程序中断,因为当我将它带回前台时,我会收到一堆 IInputConnection 警告,并且我的所有视图都是不可见的。

任何见解将不胜感激!

更新:

它无缘无故地停止了,但它又开始了。我没有更改数据库中的任何代码(日志语句除外),但我在主 Activity 中进行了一些更改,但它不应该对数据库有任何影响。这是发生的事情:

用户退出应用程序,调用 AsyncTask SaveAllTask​​。代码和上面一样,但是我添加了一些日志语句,你可以看到:

02-14 13:41:29.561: D/QBCPro:::(12104): SaveAllTask doInBackground called...
02-14 13:41:29.561: D/QBCPro:::(12104): SaveAllTask: ingredients.size() = 11
02-14 13:41:29.576: D/QBCPro:::(12104): SaveAllTask: units.size() = 12
02-14 13:41:29.616: D/QBCPro:::(12104): SaveAllTask: Saving to database...
02-14 13:41:29.671: W/IInputConnectionWrapper(12104): showStatusIcon on inactive InputConnection
02-14 13:41:29.701: D/QBCPro(12104): DatabaseHelper deleteAll() called.
02-14 13:41:29.736: D/QBCPro(12104): 11 ingredients deleted successfully
02-14 13:41:29.761: D/QBCPro(12104): 12 units deleted successfully
02-14 13:41:29.761: D/QBCPro(12104): Leaving DatabaseHelper deleteAll() method.
02-14 13:41:29.781: D/QBCPro(12104): Successfully inserted almonds (ground) into row 1
02-14 13:41:29.811: D/QBCPro(12104): Successfully inserted baking powder into row 2
02-14 13:41:29.836: D/QBCPro(12104): Successfully inserted baking soda into row 3
02-14 13:41:29.861: D/QBCPro(12104): Successfully inserted butter into row 4
02-14 13:41:29.886: D/QBCPro(12104): Successfully inserted cocoa powder into row 5
02-14 13:41:29.921: D/QBCPro(12104): Successfully inserted flour (all-purp) into row 6
02-14 13:41:29.961: D/QBCPro(12104): Successfully inserted flour (cake) into row 7
02-14 13:41:29.991: D/QBCPro(12104): Successfully inserted milk (2%) into row 8
02-14 13:41:30.021: D/QBCPro(12104): Successfully inserted sugar (br, packed) into row 9

然后它突然结束,就像上面一样,虽然奇怪的是在第 9 行而不是第 6 行。将应用程序带回前台调用 AsyncTask LoadAllTask​​,导致:

02-14 13:43:57.106: D/QBCPro:::(12532): LoadAllTask doInBackground called...
02-14 13:43:57.121: D/QBCPro:::(12532): LoadAllTask: ingredients.size() == 9
02-14 13:43:57.121: D/QBCPro:::(12532): LoadAllTask: units.size() == 0
02-14 13:43:57.121: D/QBCPro(12532): Database CLOSED! (good thing)
02-14 13:43:57.121: D/QBCPro:::(12532): LoadAllTask doInBackground completed...

我的应用程序坏了。

我认为没有必要,但以防万一,这是我的 LoadAllTask​​ 代码:

@Override
    protected Void doInBackground(Void... params) 
    {
        Log.d(TAG, "LoadAllTask doInBackground called...");
        //open database
        try 
        {
            myDbHelper.openDatabase();
        }catch(SQLException sqle)
        {    
            throw sqle;  
        }

        //get all ingredients and units
        ingredients = myDbHelper.getAllIngredients();
        units = myDbHelper.getAllUnits();

        Log.d(TAG, "LoadAllTask: ingredients.size() == " + ingredients.size());
        Log.d(TAG, "LoadAllTask: units.size() == " + units.size());

        myDbHelper.close();
        Log.d(TAG, "LoadAllTask doInBackground completed...");
        return null;
    }
4

1 回答 1

0

我通过在其中创建一个带有 AsyncTask 的 IntentService 并在 onPause 中调用它来解决此问题。它现在可以正常工作。这是 onPause 中的代码:

    Intent saveIntent = new Intent(this, SaveService.class);
    startService(saveIntent); 

这是我的 SaveService 类:

public class SaveService extends IntentService
{
    private ArrayList<Ingredient> ingredientsArray;
    private ArrayList<Unit> unitsArray;
    private DatabaseHelper dbHelper;

    public SaveService(String name) 
    {
        super(name);
    }

    public SaveService() 
    {
        super("SaveService");
    }

    @Override
    protected void onHandleIntent(Intent intent) 
    {
        //save data to database
        dbHelper = new DatabaseHelper(getApplicationContext());
        ingredientsArray = QuickBakeConverterPro.ingredients;
        unitsArray = QuickBakeConverterPro.units;

        new SaveAllTask().execute();
    }

    private class SaveAllTask extends AsyncTask<Void, Void, Void>
    {
        @Override
        protected Void doInBackground(Void... args) 
        {
            //open database
            try 
            {
                dbHelper.openDatabase();
            }catch(SQLException sqle)
            {    
                throw sqle;  
            }

            dbHelper.deleteAll();

            //add all ingredients & units
            for (int i = 0; i < ingredientsArray.size(); i++)
                dbHelper.addIngredient(ingredientsArray.get(i));
            for (int i = 0; i < unitsArray.size(); i++)
                dbHelper.addUnit(unitsArray.get(i));

            dbHelper.close();

            return null;
        }
    }//SaveAllTask
}//class SaveService
于 2013-03-05T13:52:19.513 回答