Idiom to close a cursor

后端 未结 7 1698
刺人心
刺人心 2021-02-08 03:57

Which of the following two should I be using to make sure that all the cursors are closed?

    Cursor c = getCursor(); 

    if(c!=null && c.getCount()&g         


        
7条回答
  •  走了就别回头了
    2021-02-08 04:38

    I think my answer is the best one :

        Cursor cursor = null;
    
        try {
            cursor = rsd.rawQuery(querySql, null);
            if (cursor.moveToFirst()) {
                do {
                    // select your need data from database
                } while (cursor.moveToNext());
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
                cursor = null;
            }
        }
    

提交回复
热议问题