程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> sqlite-getReadableDatabase()不能在ui線程進行?

sqlite-getReadableDatabase()不能在ui線程進行?

編輯:編程綜合問答
getReadableDatabase()不能在ui線程進行?

以前我一貫的寫法是getReadableDatabase()和getWritableDatabase() 都是在ui線程進行的,而且在android4.0以上版本都沒有任何問題,最近換了個android2.2的設備測試,發現就報錯了,異常是SQLiteException 並提示database is locked這些信息,檢查我對數據庫的操作只不過是寫完數據後讀數據,應該不存在數據庫被鎖的問題。

思前想後,在SQLiteOpenHelper 源代碼中發現到getReadableDatabase()的如何注解
/*
* Create and/or open a database. This will be the same object returned by
* {@link #getWritableDatabase} unless some problem, such as a full disk,
* requires the database to be opened read-only. In that case, a read-only
* database object will be returned. If the problem is fixed, a future call
* to {@link #getWritableDatabase} may succeed, in which case the read-only
* database object will be closed and the read/write object will be returned
* in the future.

** <p class="caution">Like {@link #getWritableDatabase}, this method may
 * take a long time to return, so you should not call it from the
 * application main thread, including from
 * {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.*

 * @throws SQLiteException if the database cannot be opened
 * @return a database object valid until {@link #getWritableDatabase}
 *     or {@link #close} is called.
 */

留意高亮部分,注解說不要寫在應用的主線程,包括contentprovider的oncreate方法,我好奇的是 網上大家都是直接寫在ui線程,而且看上去是可行的 為什麼官方的api注解卻提示不可以。還有一個奇怪的問題,就是我說過的遇到的那個異常並不是必定的,是偶爾出現的
so 說下你們的見解吧

最佳回答:


那就應該是鎖了! 雖然文檔是提示你應該在子線程中做,但是主線程做也不會影響的! 應該是有別的線程在異步操作這個數據庫,如果別的線程中關閉了數據庫,而你這裡卻在對數據庫操作,也會出現這個bug

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved