• 在Activity中为什么要用managedQuery()


          刚開始接触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的情况了。


  • 相关阅读:
    OPENDS 5.0 OPEN SOURCE DRIVING SIMULATION
    求推荐计算机图形学,渲染,shader方面的入门书籍
    交通流仿真|万字综述
    Hbase数据备份导入导出
    hadoop用法之mapreduce的应用场景
    mapreduce
    google论文四 Bigtable:结构化数据的分布式存储系统(上)
    经典SQL语句大全
    socket编程之登峰造极完成端口
    BCGControlBar的使用
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4003032.html
Copyright © 2020-2023  润新知