在 SQLite 中更新数据时应用程序冻结

Apps freeze when update data in SQLite

我能够将数据插入 SQLite,然后用数据填充我的 ListView

但是每当我尝试更新数据时,我的 phone 就会冻结,我需要重新打开应用程序才能继续

这也发生在其他设备上,这意味着我的代码中有些东西导致冻结

我的 Java 代码更新数据

boolean editContent(){
    SQLiteDatabase db = myDb.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(myDb.COL_NAME, savedName.getText().toString());
    contentValues.put(myDb.COL_IP, savedIP.getText().toString());
    contentValues.put(myDb.COL_PORT, savedPort.getText().toString());
    contentValues.put(myDb.COL_USER_ID, savedUserID.getText().toString());
    contentValues.put(myDb.COL_USER_PASS, savedUserPass.getText().toString());

    //db.update(myDb.TABLE_NAME, contentValues , "ID=" + thisID, null); // problem
    db.update(myDb.TABLE_NAME, contentValues , "ID = '"+ thisID +"'", null);// solution

    if (editContent() == true){
        Toast.makeText(editActivity.this, "Edit Done!", Toast.LENGTH_SHORT).show();
        return true;
    }else{
        Toast.makeText(editActivity.this, "Edit failed!", Toast.LENGTH_SHORT).show();
        return false;
    }
}

只有 logcat 如果我等一会儿,我得到的是

09-09 16:42:14.896 8903-8918/app.my.my W/art: Suspending all threads took: 29.860ms
09-09 16:42:14.922 8903-8924/app.my.my I/art: Background sticky concurrent mark sweep GC freed 23583(1062KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 125MB/127MB, paused 21.123ms total 75.895ms
09-09 16:42:17.671 8903-8924/app.my.my I/art: Background sticky concurrent mark sweep GC freed 13051(586KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 126MB/127MB, paused 19.706ms total 84.816ms
09-09 16:42:19.058 8903-8924/app.my.my I/art: Background sticky concurrent mark sweep GC freed 7133(319KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 126MB/127MB, paused 19.733ms total 71.671ms
09-09 16:42:20.202 8903-8924/app.my.my I/art: Background sticky concurrent mark sweep GC freed 3855(171KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 126MB/127MB, paused 20.306ms total 70.792ms
09-09 16:42:20.668 8903-8924/app.my.my I/art: Background partial concurrent mark sweep GC freed 4903(287KB) AllocSpace objects, 3(37MB) LOS objects, 4% free, 88MB/92MB, paused 20.672ms total 133.448ms
09-09 16:42:31.064 8903-8924/app.my.my I/art: Background sticky concurrent mark sweep GC freed 48833(2MB) AllocSpace objects, 0(0B) LOS objects, 2% free, 90MB/92MB, paused 25.972ms total 112.428ms
09-09 16:42:36.098 8903-8924/app.my.my I/art: Background partial concurrent mark sweep GC freed 23981(1084KB) AllocSpace objects, 0(0B) LOS objects, 4% free, 91MB/95MB, paused 28.538ms total 150.916ms
09-09 16:42:36.098 8903-8923/app.my.my I/art: WaitForGcToComplete blocked for 34.288ms for cause HeapTrim

我不知道为什么,但大多数时候我在 post SO

上的问题后找到了问题的解决方案

问题出在这里

db.update(myDb.TABLE_NAME, contentValues , "ID = '"+ thisID +"'", null);// solution

if (editContent() == true){
    Toast.makeText(editActivity.this, "Edit Done!", Toast.LENGTH_SHORT).show();
    return true;
}else{
    Toast.makeText(editActivity.this, "Edit failed!", Toast.LENGTH_SHORT).show();
    return false;
}

我通过这样做设法解决了这个问题

int isUpdated = db.update(myDb.TABLE_NAME, contentValues , "ID = '"+ thisID +"'", null);// solution

if (isUpdated > 0){
    Toast.makeText(editActivity.this, "Edit Done!", Toast.LENGTH_SHORT).show();
    return true;
}else{
    Toast.makeText(editActivity.this, "Edit failed!", Toast.LENGTH_SHORT).show();
    return false;
}

看来 editContent boolean 是冻结的原因

您似乎在创建一个无限循环。即 editContent() 将继续调用 editContent(),因此永远不会退出函数。是的,你解决它的方法是正确的。