我的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;
}
答案 0 :(得分: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