【发布时间】:2017-04-28 07:39:38
【问题描述】:
我在 AsyncTask 中从服务器下载数据,然后将其写入数据库。下面是 doInBackground 方法的一部分:
DBConnection db = new DBConnection(context);
JSONObject json = new JSONObject(s);
JSONObject cataloglist = json.getJSONObject("cataloglist");
JSONArray array = cataloglist.names();
db.clearNomenclature();
for(int n = 0; n < array.length(); n++)
{
JSONObject object = cataloglist.getJSONObject(array.getString(n));
db.insertNomenclature(object.getInt("version"), object.getString("name"), object.getString("uuid"), object.getString("measure"));
}
这里我访问了 DBConnection 类,在 DBConnection 类的 insertNomenclature 方法中,我将数据插入到数据库中,如下所示:
dbOpenHelper = new ExternalDbOpenHelper(context, "DB");
database = dbOpenHelper.getWritableDatabase();
database.execSQL("CREATE TABLE IF NOT EXISTS "+"nomenclature"+
" ("+"ID"+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
"name TEXT, version INTEGER, measure UUID, uuid UUID)");
ContentValues cv = new ContentValues();
cv.put("name", name);
cv.put("version", version);
cv.put("uuid", uuid);
cv.put("measure", measure);
database.insert("nomenclature", null, cv);
database.close();
在日志中我经常收到这样的消息:
12-12 12:11:40.825 26770-26770/com.example.sanzharaubakir.exgroup I/Choreographer: Skipped 4205 frames! The application may be doing too much work on its main thread.
我希望在后台执行数据库插入,但显然它以某种方式在 UI 线程上工作。如何将数据库插入移动到另一个线程?
【问题讨论】:
-
doInBackground 运行在一个单独的线程上,你确定是运行造成的跳帧吗?
-
当我删除这一行时 db.insertNomenclature(object.getInt("version"), object.getString("name"), object.getString("uuid"), object.getString("measure) "));我没有收到这样的消息
标签: android multithreading sqlite android-asynctask