在getCursor方法中关闭Cursor是危险的吗?

时间:2013-09-03 14:57:01

标签: android android-sqlite android-cursor

我制作了自己的getCursor方法,看起来像这样

protected Cursor getCursor(String selectQuery) {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    return cursor;
}

因为在我返回之前我无法调用cursor.close(),所以将光标打开是否很危险?或者只要方法返回其对象,它就会关闭?

2 个答案:

答案 0 :(得分:1)

你会得到一些“TooManyCursorsException” - 或something like that。在你使用光标的地方你可以尝试{} finally {}块,最后你可以说:

if(cursor != null) {
   cursor.close();
}

我从网络编程中使用,在从DAO读取数据时创建模型pojos并在up-levels中使用它。我永远不会将Cursor发送到上面的层。

答案 1 :(得分:1)

如果您的光标需要活动生命周期,请使用call Activity.startManagingCursor(Cursor c),否则应明确关闭光标。