如何创建一个线程安全的ContentProvider? [英] How to create a Thread-safe ContentProvider?

查看:1988
本文介绍了如何创建一个线程安全的ContentProvider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android的文件说,

  

ContentProvider的方法可以从各种ContentResolver的被称为   在不同的进程和线程的目的,它们必须实施   在一个线程安全的方式

和我发现这个职位上#1 <一href="http://stackoverflow.com/questions/6912793/android-sqlite-content-providers-and-multithreading">Android - sqlite的内容提供商和多线程 它说,这是线程安全已??

所以,只是想知道如何创建一个线程安全的的ContentProvider ?它是不够的,如果我做的插入/更新/删除方法 syncronized

 公共同步开放的插入(URI URI,ContentValues​​值){

}
 

解决方案

您可以让每一个方法同步但要确保它在此之前是绝对必要的。在情况下,底层数据源已经是线程安全的决策同步的方法可能是昂贵的。请参阅我的<一个href="http://www.androiddesignpatterns.com/2012/10/sqlite-contentprovider-thread-safety.html">blog交 关于此主题的更多信息。

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) {

}

解决方案

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.

这篇关于如何创建一个线程安全的ContentProvider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆