Correct implementation of asynchronous Android SQLite database

送分小仙女□ 提交于 2019-12-10 10:24:26

问题


I've Googled around and have found multiple ways of accessing a local SQLite database asynchronously:

  • AsyncTask
  • CursorLoader (I've already used this for a query to retrieve my contacts information, but I'm not sure how this will translate to a SQLiteOpenHelper subclass with multiple queries)
  • ContentProvider - Not sure if it's overkill, the database will only be need from within the app

What is the best practice? I currently have a SQLiteOpenHelper subclass which contains the basic table creation/upgrade/etc. logic.


回答1:


CursorLoader only supports queries. You may still want to use it and leverage the Loader framework if you are displaying data in your UI. CursorLoader is intended to be used with a ContentProvider, but you can instead copy the source code and modify it to take an SQLiteDatabase instead of a Context and query the database instead of a ContentResolver inside the loadInBackground() method.

For write operations, you have several options to consider (these are not mutually exclusive, you may end up using more than one depending on the situation):

  • AsyncTask
  • Handler
  • IntentService
  • AsyncQueryHandler
  • Java threading/executor framework

I typically use AsyncTask for one-off operations that may have minor UI implications (e.g. showing and hiding some indicator on screen). Note that AsyncTasks run serially on a single worker thread, unless you supply your own Executor to execute().

IntentService is useful in that it queues all of its start commands and executes them serially on a worker thread, and automatically shuts down when finished with all of them. It's a Service, so it runs separately from any Activities/UI components, but that also means there's some overhead since it is an application component that needs to be created and started by the system. I like them for batch operations or operations that are scheduled sometime in the future.

AsyncQueryHandler does more than just queries (despite how it's named), but like CursorLoader it expects you are communicating with a ContentProvider. You can use a single one to process different kinds of operations. It's important to note that ContentProvider by itself does not provide asynchronous processing, you have to call it from a background thread, which is what AsyncQueryHandler does.

Finally, good old Java threading and the executor framework works just fine, though you'll probably want to have some kind of application component for them to run in (likely a Service, but if that's the case you might just go with IntentService above anyway).




回答2:


  • CursorLoader is only for read data asynchronously
  • ContentProvider is to provide data to others applications, there's no option for synchronously or asynchronously access.

I suggest you to use AsyncTask



来源:https://stackoverflow.com/questions/34325327/correct-implementation-of-asynchronous-android-sqlite-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!