Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - sqlite content providers and multithreading

I'm a little confused about content providers. If I have multiple activities in my application do they each get their own instance of the content provider? it's just essentially a class/interface?

In one activity I will have many threads simultaneously writing to the database. How do I deal with allowing one thread to write at a time?

Do I just catch SQLiteDatabaseLockedException, put the thread to sleep then retry? Or is there a better way?

Are the database locks released when an activity pauses/is destroyed? If so could I just create a synchronized lock against the content provider itself?

like image 708
user Avatar asked Aug 02 '11 13:08

user


People also ask

Is SQLite thread safe Android?

In this mode, SQLite can be safely used by multiple threads provided that no single database connection is used simultaneously in two or more threads. Serialized. In serialized mode, SQLite can be safely used by multiple threads with no restriction.

What is the difference between content provider and SQLite database in Android?

What is the exact difference between "Content-Provider" and "SQLite Database"? ContentProvider is a facade -- an API you can implement that exposes databases to other processes. It can be implemented in a way where the data is stored in a SQLite database, but it does not have to be.

Is SQLite database thread safe?

Ok I reread this answer - maybe you should put this answer to actual state instead of [right][wrong][wrong] it's very confusing. The simple answer is NO. Its not thread safe - that's it. stackoverflow.com/questions/12758655/…

Is SQLite a content provider?

A content provider behaves very much like a database where you can query it, edit its content, as well as add or delete content using insert(), update(), delete(), and query() methods. In most cases this data is stored in an SQlite database.


2 Answers

If you work directly with databases and have multiple writers from different threads you may run into concurrency issues.

The ContentProvider can be accessed from several programs at the same time, therefore you must implement the access thread-safe. The easiest way is to use the keyword synchronized in front of all methods of the ContentProvider, so that only one thread can access these methods at the same time.

If you do not require that Android synchronizes data access to the ContentProvider, set the android:multiprocess=true attribute in your <provider> definition in the AndroidManifest.xml file. This permits an instance of the provider to be created in each client process, eliminating the need to perform interprocess communication (IPC).

like image 50
Alex Lockwood Avatar answered Nov 09 '22 01:11

Alex Lockwood


Oops, I lost my unregistered user cookie so can't vote Femi's answer correct.

The documentation http://developer.android.com/guide/topics/providers/content-providers.html confirms this with "When a query is initiated, the Android system identifies the content provider that's the target of the query and makes sure that it is up and running. The system instantiates all ContentProvider objects; you never need to do it on your own. In fact, you never deal directly with ContentProvider objects at all. Typically, there's just a single instance of each type of ContentProvider. But it can communicate with multiple ContentResolver objects in different applications and processes. The interaction between processes is handled by the ContentResolver and ContentProvider classes. "

like image 33
user Avatar answered Nov 09 '22 02:11

user