zoukankan      html  css  js  c++  java
  • 在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的情况了。


  • 相关阅读:
    SuiteScript > Script Queue Monitor (Beta)
    Mac > MacBook Pro的移动硬盘方案
    财务报表 > 现金流表的直接法,间接法,Cash Flow from Operating Activites
    税收基础知识 > 三税(营业税, 增值税, 所得税) + 关税
    Netsuite > Hierarchy of transactions in Inventory cost calculation
    Javascript > Eclipse > problems encountered during text search
    Javascript > Eclipse > 自动代码规范化
    嵌入式Linux > 简易安装思路,步骤记录
    Netsuite > Employee Record Name维护规则
    POSTMAN as debugger for integration APPs
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4008082.html
Copyright © 2011-2022 走看看