刚开始接触android的时候,每次用数据库都会犹豫使用哪种方式,一种是getContentResolver().query(...),另一种是managedQuery(...),后来习惯了使用前一种,后一种就被我遗忘了,但是在实际做项目时,有时数据库经常会报cursor not close的warning,有的cursor你可以手动关闭,但是有一些就不可以了,比如当前是个listActivity,他的adapter是个cursorAdapter,这里的cursor就不能关掉,当然你可以在onDestroy中做关闭的操作,可是我比较习惯把cursor定义为局部变量,不是全局可见的,这样的话你就不能在onDestroy中关闭了。
后来就查看源代码,发现manageQuery可以为你维护这个cursor。在你退出activity时为你自动关闭,其实他的原理也很简单,看源码:
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的情况了。
本文解释了Android中如何使用managedQuery方法自动管理Cursor,防止内存泄漏问题,并提供了源码解析,包括Cursor队列操作和在Activity销毁时自动关闭Cursor的机制。
695

被折叠的 条评论
为什么被折叠?



