原文
关注微博:InfoStation
One thing that I’ve noticed other AndroID developers having trouble with is properly setting up their sqliteDatabase. Often times,I come across questions on StackOverflow asking about error messages such as,
E/Database(234): Leak found
E/Database(234): Caused by: java.lang.IllegalStateException: sqliteDatabase created and never closed
As you have probably figured out,this exception is thrown when you have opened more sqliteDatabase instances than you have closed. Managing the database can be complicated when first starting out with AndroID development,especially to those who are just beginning to understand the Activity lifecycle. The easIEst solution is to make your database instance a singleton instance across the entire application’s lifecycle. This will ensure that no leaks occur,and will make your life a lot easIEr since it eliminates the possibility of forgetting to close your database as you code.
Here are two examples that illustrates three possible approaches in managing your singleton database. These will ensure safe access to the database throughout the application.
Approach #1: Use a Singleton to Instantiate the sqliteOpenHelperDeclare your database helper as a static instance variable and use the Singleton pattern to guarantee the singleton property. The sample code below should give you a good IDea on how to go about designing the DatabaseHelper class correctly.
The static getInstance() method ensures that only one DatabaseHelper will ever exist at any given time. If the sInstance object has not been initialized,one will be created. If one has already been created then it will simply be returned. You should not initialize your helper object using with new DatabaseHelper(context)! Instead,always use DatabaseHelper.getInstance(context),as it guarantees that only one database helper will exist across the entire application’s lifecycle.
public class DatabaseHelper extends sqliteOpenHelper { private static DatabaseHelper sInstance; private static final String DATABASE_name = "database_name"; private static final String DATABASE_table = "table_name"; private static final int DATABASE_VERSION = 1; public static synchronized DatabaseHelper getInstance(Context context) { // Use the application context,which will ensure that you // don't accIDentally leak an Activity's context. // See this article for more information: http://bit.ly/6LRzfx if (sInstance == null) { sInstance = new DatabaseHelper(context.getApplicationContext()); } return sInstance; } /** * Constructor should be private to prevent direct instantiation. * make call to static method "getInstance()" instead. */ private DatabaseHelper(Context context) { super(context,DATABASE_name,null,DATABASE_VERSION); }}Approach #2: Wrap the sqliteDatabase in a ContentProvIDer
This is also a nice approach. For one,the new CursorLoader class requires ContentProvIDers,so if you want an Activity or Fragment to implement LoaderManager.LoaderCallbacks with a CursorLoader (as discussed in this post),you’ll need to implement a ContentProvIDer for your application. Further,you don’t need to worry about making a singleton database helper with ContentProvIDers. Simply call getContentResolver() from the Activity and the system will take care of everything for you (in other words,there is no need for designing a Singleton pattern to prevent multiple instances from being created).
Leave a comment if this helped or if you have any questions!
总结以上是内存溢出为你收集整理的Correctly Managing your SQLite Database全部内容,希望文章能够帮你解决Correctly Managing your SQLite Database所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)