2011-11-21 57 views
1

現在我控制我的遊標,如下所示。我想知道使用startManagingCursor()的好處是什麼。就目前而言,我有很多遊標,每一個知道,然後得到一個錯誤與他們做。如果不是更好的做法,這會有好處嗎?使用startManagingCursor()的優點和缺點

Cursor c = db.rawQuery("GENERIC QUERY" , null); 
c.moveToFirst(); 
numval = c.getInt(c.getColumnIndex("_id"));     
c.close(); 

回答

1

所有startManagingCursor首先是deperecated API http://developer.android.com/reference/android/app/Activity.html#startManagingCursor(android.database.Cursor

現在我們必須使用CursorLoader類LoaderManager。爲了回答你的問題,如果活動正在管理光標,那麼它可以在屏幕方向發生時進行優化。 Activitity會在其生命週期中關注遊標的生命週期。以下是android doc的片段。

This method allows the activity to take care of managing the given Cursor's lifecycle for you based on the activity's lifecycle. That is, when the activity is stopped it will automatically call deactivate() on the given Cursor, and when it is later restarted it will call requery() for you. When the activity is destroyed, all managed Cursors will be closed automatically

0

startManagingCursor捆綁光標生命週期的活動的生命週期。這包括在您的活動恢復時自動重新查詢。我傾向於避免使用它,因爲我不一定希望每次重新啓動我的活動時重新執行查詢。

就最佳實踐而言,如果您希望在您的活動暫停時更新數據庫,但即使如此,我仍然偏向於管理自己的光標,但它可能更有意義。我還希望讓遊標在短時間內保持打開狀態,所以您的示例與我傾向於使用的模式相匹配。