Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Thread-safe ContentProvider?

Android documentation says

ContentProvider methods can be called from various ContentResolver objects in different processes and threads, they must be implemented in a thread-safe manner

And I found this post on Stackoverflow Android - sqlite content providers and multithreading which says it's thread safe already ??

So, Just wondering how to create a thread-safe ContentProvider ? Is it enough if I make the insert/update/delete methods syncronized

public synchronized Uri insert (Uri uri, ContentValues values) {

}
like image 900
kakopappa Avatar asked Sep 20 '11 08:09

kakopappa


People also ask

Is content provider thread-safe?

Conclusion. Although the ContentProvider lacks in thread-safety, often times you will find that no further action is required on your part with respect to preventing potential race conditions.

How do I make a program thread-safe in C++?

An object is thread-safe for reading from multiple threads. For example, given an object A, it is safe to read A from thread 1 and from thread 2 simultaneously. If an object is being written to by one thread, then all reads and writes to that object on the same or other threads must be protected.

How do I create a custom content provider?

Create Content Provider First of all you need to create a Content Provider class that extends the ContentProviderbaseclass. Second, you need to define your content provider URI address which will be used to access the content. Next you will need to create your own database to keep the content.


1 Answers

You could make every method synchronized, but make sure it is absolutely necessary before you do. In cases where the underlying data source is already thread-safe making the methods synchronized could be costly. See my blog post on this topic for more information.

like image 72
Alex Lockwood Avatar answered Oct 17 '22 14:10

Alex Lockwood