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) {
}
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With