A few questions about SQLite database courses in Android

To implement database access in my application, I followed the Lars Vogel tutorial , but I am very confused about a few things ...

1) Each time fetchTodo is called, a new cursor will be created and returned. Keep the previous cursor for the garbage collector. So, if I do not use startManagingCursor or even CursorLoader , if I need it, should I call .close() cursor when I finish with it? Outside the scope of fetchTodo , of course, an example:

 Cursor cursor = mNotesAdapter.fetchTodo(); // do something... cursor.close(); 

I ended up with this cursor and a new one will be created the next time I extract it, should I close it like this, or should I leave it to the garbage collector? Although I think I'm talking about two things, completely different ... That is, should it be closed, as in the above example, or not?

2) Cursor also has a .deactivate() method, and the documentation says that it uses less resources (than active cursors). When should I use this? For example, in my application, I have a ListActivity , which is populated through the SimpleCursorAdapter (the code initialization for this is called only once). The cursor used is a member variable of the class, because I need it outside the method that populates the list. I need it to query the database when something has been removed from it. But until the record is deleted, this is a user action and may take some time, should the cursor be disabled in the meantime? Because it will be active again when I call .requery() again. Or SimpleCursorAdapter will stop working because the cursor is not active?

EDIT: I just tested this and found out that after setting up the cursor adapter, I cannot call deactivate() . The list will be empty if the cursor is not active, so it must remain active as long as ListActivity is displayed. In the end, we should just let startManagingCursor handle it. Or the new CursorLoader .

3) I know that startManagingCursor / stopManagingCursor deprecated, but I do not configure Honeycomb (at least for now), and I do not want to deal with the new CursorLoader yet. But in the above guide, startManagingCursor used everywhere, but stopManagingCursor never called once. Why not? Is there any Android with this in its own way? Any situation I should call stopManagingCursor ?

+6
source share
1 answer

Edit: Updated answer to reflect updated question 1:

1) Each time a fetchTodo call is made, a new cursor is created and returned. Keep the previous cursor for the garbage collector. Therefore, if I do not use startManagingCursor or even CursorLoader for this is important, should I call the cursor .close () on the cursor when I am done with this?

Yes, you must definitely say Android on startManagingCursor() , use LoaderManager / CursorLoader or close() it yourself. This will not lead to a memory leak, GC will not help with this, since it has its own resources for Cursor (for example, files are processed in the database).

2) The cursor also has a .deactive () method, and the documentation says it uses less resources (than active cursors). When should I use this? ...

EDIT for other readers: OP found the answer and posted it in his question. The following remains:

I never used deactivate() (there is no deactive() ), maybe someone can explain this. If you want a truly painless request / update, check out the LoaderManager structure - this is not only for Honeycomb: using the compatibility library, you can use the LoaderManager (and Fragments ) up to Android 1.6. This is not only the code for you, but also completely unloads these things on Android, much more than startManagingCursor() .

EDIT2: some notes on LoaderManager

Developer.android.com has LoaderManager , but it's pretty ... hard and hard to understand for the first time, like most tutorials. I also had to dig a lot, the best all-in-one stop I've found so far is http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/ (plus all javadocs and compat source lib which you can find) --- The LoaderManager method LoaderManager very similar to managed dialogs (now also obsolete, replaced by DialogFragment ) with their onCreateDialog , onPrepareDialog , where you simply point Android to "show dialog # 123" and then Android calls your code with this identifier; the same for bootloaders: "load loader # 123", Android calls onCreateLoader() .

The only obvious flaw initially is that the LoaderManager relies heavily on the ContentProvider , and some people really don't like it. Of course, this is additional training and code, but as soon as you have a ContentProvider for your own data (even if they are used only privately in your application), all data bindings to the view are a breeze with CursorLoader . IMHO, there is a slight difference between rolling your own "content provider" and actually implementing ContentProvider - but this is just my opinion very controversial :)

3) I know that startManagingCursor / stopManagingCursor is outdated but I am not targeting Honeycomb (at least for now), and now I don’t want to deal with the new CursorLoader. But in the above, startManagingCursor is used everywhere, but stopManagingCursor is never called once. Why not? Does Android do this in it in its own way? Any situation I should call stopManagingCursor?

Once you call startManagingCursor() , Cursor no longer your problem. Android will take care to close the cursor when your Activity is destroyed (user will move, change orientation, ...). There is no need to map the startManagingCursor() call to the stopManagingCursor() call - you usually don't want to take charge of managing Cursor again once you get rid of it.

+9
source

Source: https://habr.com/ru/post/898322/


All Articles