public class OpenHelperManager extends Object
OrmLiteSqliteOpenHelper
class that is used to manage the database
connection. The helper instance will be kept in a static field and only released once its internal usage count goes
to 0.
The SQLiteOpenHelper
and database classes maintain one connection under the hood, and prevent locks in the
java code. Creating multiple connections can potentially be a source of trouble. This class shares the same
connection instance between multiple clients, which will allow multiple activities and services to run at the same
time.
Every time you use the helper, you should call getHelper(Context)
or getHelper(Context, Class)
.
When you are done with the helper you should call releaseHelper()
.Constructor and Description |
---|
OpenHelperManager() |
Modifier and Type | Method and Description |
---|---|
static OrmLiteSqliteOpenHelper |
getHelper(android.content.Context context)
Deprecated.
Should use
getHelper(Context, Class) |
static <T extends OrmLiteSqliteOpenHelper> |
getHelper(android.content.Context context,
Class<T> openHelperClass)
Create a static instance of our open helper from the helper class.
|
static void |
releaseHelper()
Release the helper that was previously returned by a call
getHelper(Context) or
getHelper(Context, Class) . |
static void |
setHelper(OrmLiteSqliteOpenHelper helper)
Set the helper for the manager.
|
static void |
setOpenHelperClass(Class<? extends OrmLiteSqliteOpenHelper> openHelperClass)
If you are _not_ using the
OrmLiteBaseActivity type classes then you will need to call this in a static
method in your code. |
public static void setOpenHelperClass(Class<? extends OrmLiteSqliteOpenHelper> openHelperClass)
OrmLiteBaseActivity
type classes then you will need to call this in a static
method in your code.public static void setHelper(OrmLiteSqliteOpenHelper helper)
public static <T extends OrmLiteSqliteOpenHelper> T getHelper(android.content.Context context, Class<T> openHelperClass)
releaseHelper()
. This should be called during an
onCreate() type of method when the application or service is starting. The caller should then keep the helper
around until it is shutting down when releaseHelper()
should be called.@Deprecated public static OrmLiteSqliteOpenHelper getHelper(android.content.Context context)
getHelper(Context, Class)
Similar to getHelper(Context, Class)
(which is recommended) except we have to find the helper class
through other means. This method requires that the Context be a class that extends one of ORMLite's Android base
classes such as OrmLiteBaseActivity
. Either that or the helper class needs to be set in the strings.xml.
To find the helper class, this does the following:
setOpenHelperClass(Class)
, it will be used to construct a
helper.OrmLiteBaseActivity
and other base classes.public static void releaseHelper()
getHelper(Context)
or
getHelper(Context, Class)
. This will decrement the usage counter and close the helper if the counter is
0.
WARNING: This should be called in an onDestroy() type of method when your application or service is
terminating or if your code is no longer going to use the helper or derived DAOs in any way. _Don't_ call this
method if you expect to call getHelper(Context)
again before the application terminates.
This documentation is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.