Concurrent writing to android database (from multiple services)?

廉价感情. 提交于 2019-11-27 21:29:24

You need to use a single SQLiteDatabase object, across all threads (and their hosting components), to get thread safety. Make your DBHelper be a singleton, or use a ContentProvider, to achieve this effect.

stuckless

The ContentProvider was created for this reason. You can call it from multiple threads for insert/update/delete operations.

http://developer.android.com/reference/android/content/ContentProvider.html

Many people feel that you should only want to use a ContentProvider if you want to share data. That is a great benefit of a ContentProvider, but it's not the only benefit. The major benefit is that when using a ContentProvider, Android will manage the database connections for you.

This is a good tutorial on Content Providers

http://www.vogella.com/articles/AndroidSQLite/article.html

Note: While the JavaDoc for ContentProvider does state that it is REQUIRED in order to share data, it does not mean that it should ONLY be used to share data. In the Application Fundamentals documentation, it has this to say about ContentProviders as well..

Content providers are also useful for reading and writing data that is private to your application and not shared. For example, the Note Pad sample application uses a content provider to save notes.

http://developer.android.com/guide/topics/fundamentals.html#lcycles

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