是否确定有SQLiteOpenHelper的一个实例,所有活动在一个Android应用程序共享的? [英] Is it OK to have one instance of SQLiteOpenHelper shared by all Activities in an Android application?

查看:116
本文介绍了是否确定有SQLiteOpenHelper的一个实例,所有活动在一个Android应用程序共享的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

难道是确定有SQLiteOpenHelper的单个实例作为一个子类的应用程序的成员,并有需要SQLiteDatabase的一个实例的所有活动,从一个帮手得到它?

Would it be OK to have a single instance of SQLiteOpenHelper as a member of a subclassed Application, and have all Activities that need an instance of SQLiteDatabase get it from the one helper?

推荐答案

有一个 SQLiteOpenHelper 实例可以在线程的情况下帮助。由于所有的线程将共享共同的 SQLiteDatabase ,操作的同步提供。

Having a single SQLiteOpenHelper instance can help in threading cases. Since all threads would share the common SQLiteDatabase, synchronization of operations is provided.

不过,我不会做的应用程序子类。只要有一个静态数据成员就是你的 SQLiteOpenHelper 。这两种方法让你从任何地方访问的东西。然而,只有的一个的的应用程序,使之更难以供你使用的的子类的子类应用程序(例如,GreenDroid需要一个IIRC)。使用静态数据成员避免了。但是,确实使用了应用程序 上下文实例化这个静态时 SQLiteOpenHelper (构造函数参数),这样你就不会泄露了一些其他的上下文

However, I wouldn't make a subclass of Application. Just have a static data member that is your SQLiteOpenHelper. Both approaches give you something accessible from anywhere. However, there is only one subclass of Application, making it more difficult for you to use other subclasses of Application (e.g., GreenDroid requires one IIRC). Using a static data member avoids that. However, do use the Application Context when instantiating this static SQLiteOpenHelper (constructor parameter), so you do not leak some other Context.

和,在情况下,你不处理多线程,可以只使用每个组件有一个 SQLiteOpenHelper 例如避免任何可能的内存泄漏问题。然而,在实践中,你的的要处理多个线程(例如,装载机),所以这个建议是只与琐碎的应用,例如那些在一些书籍中...: - )

And, in cases where you aren't dealing with multiple threads, you can avoid any possible memory leak issues by just using one SQLiteOpenHelper instance per component. However, in practice, you should be dealing with multiple threads (e.g., a Loader), so this recommendation is only relevant for trivial applications, such as those found in some books... :-)

这篇关于是否确定有SQLiteOpenHelper的一个实例,所有活动在一个Android应用程序共享的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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