在Activity中为什么要用managedQuery()

it2022-05-09  75

      刚開始接触android的时候,每次用数据库都会犹豫使用哪种方式,一种是getContentResolver().query(...),还有一种是managedQuery(...),后来习惯了使用前一种,后一种就被我遗忘了,可是在实际做项目时,有时数据库常常会报cursor not close的warning,有的cursor你能够手动关闭,可是有一些就不能够了,比方当前是个listActivity,他的adapter是个cursorAdapter,这里的cursor就不能关掉,当然你能够在onDestroy中做关闭的操作,可是我比較习惯把cursor定义为局部变量,不是全局可见的,这种话你就不能在onDestroy中关闭了。

     后来就查看源代码,发现manageQuery能够为你维护这个cursor。在你退出activity时为你自己主动关闭,事实上他的原理也非常easy,看源代码:

Activity.java

private static final class ManagedCursor {         ManagedCursor(Cursor cursor) {             mCursor = cursor;             mReleased = false;             mUpdated = false;         }         private final Cursor mCursor;         private boolean mReleased;         private boolean mUpdated;     }     private final ArrayList<ManagedCursor> mManagedCursors =         new ArrayList<ManagedCursor>();

这里定义了一个Cursor队列,这个Cursor是被封装的。

以下是对这个队列的操作:

 public final Cursor managedQuery(Uri uri,                                      String[] projection,                                      String selection,                                      String[] selectionArgs,                                      String sortOrder)     {         Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);         if (c != null) {             startManagingCursor(c);         }         return c;     }

//加入,注意要加锁

 public void startManagingCursor(Cursor c) {         synchronized (mManagedCursors) {             mManagedCursors.add(new ManagedCursor(c));         }     }

//在这个activity结束的时候,会调用onDestroy,所以清理的工作应该在那里

protected void onDestroy() {        //省略。。。。         // close any cursors we are managing.         synchronized (mManagedCursors) {             int numCursors = mManagedCursors.size();             for (int i = 0; i < numCursors; i++) {                 ManagedCursor c = mManagedCursors.get(i);                 if (c != null) {                     c.mCursor.close();                 }             }             mManagedCursors.clear();         }    //省略。。。。     }

近期又看源代码,发现能够不用managedQuery,能够用普通的query,然后执行 startManagingCursor(cursor),相同能够把cursor交给系统去管理,不用操心cursor没有close的情况了。

转载于:https://www.cnblogs.com/mengfanrong/p/3929462.html

相关资源:数据结构—成绩单生成器

最新回复(0)