Android中Cursor關閉的問題

來源:互聯網
上載者:User

 Cursor是Android查詢資料後得到的一個管理資料集合的類,正常情況下,如果查詢得到的資料量較小時不會有記憶體問題,而且虛擬機器能夠保證Cusor最終會被釋放掉。

    然而如果Cursor的資料量特表大,特別是如果裡面有Blob資訊時,應該保證Cursor佔用的記憶體被及時的釋放掉,而不是等待GC來處理。並且Android明顯是傾向於編程者手動的將Cursor close掉,因為在原始碼中我們發現,如果等到記憶體回收行程來回收時,也就是如果不手動關閉,系統會報錯,會給使用者以錯誤提示。$ \/ D4 E, P, R! t/ r- C+ Z

    所以我們使用Cursor的方式一般如下:& A) w0 J6 w. z' u' R- ]- t0 |% \) p

Cursor cursor = null;

try{

    cursor = mContext.getContentResolver().query(uri,null,null,null,null);

    if(cursor != null){

        cursor.moveToFirst();

    //do something

    }

}catch(Exception e){

    e.printStatckTrace();

}finally{

    if(cursor != null){

        cursor.close();

    }

}

有一種情況下,我們不能直接將Cursor關閉掉,這就是在CursorAdapter中應用的情況,但是注意,CursorAdapter在Acivity結束時並沒有自動的將Cursor關閉掉,因此,你需要在onDestroy函數中,手動關閉。

    @Override  3 o, S0 X( j( Y/ B$ ]. [1 {2 s
    protected void onDestroy() {        ' [* T. [. _1 Q0 @7 O; \
        if (mAdapter != null && mAdapter.getCurosr() != null) {  1 ~7 }2 l& E4 Y. ?) J/ V! n( K+ L: c
            mAdapter.getCursor().close();  
        }  ( `9 d- E" U% i" S/ V2 X* N
        super.onDestroy();   
    }

CursorAdapter中的changeCursor函數,會將原來的Cursor釋放掉,並替換為新的Cursor,所以你不用擔心原來的Cursor沒有被關閉。

  你可能會想到使用Activity的managedQuery來產生Cursor,這樣Cursor就會與Acitivity的生命週期一致了,多麼完美的解決方案!然而事實上managedQuery也有很大的局限性。

    managedQuery產生的Cursor必須確保不會被替換,因為可能很多程式事實上查詢條件都是不確定的,因此我們經常會用新查詢的Cursor來替換掉原先的Cursor。因此這種方法適用範圍也是很小。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.