I use OrmLite and I want to get the helper class (and then get a dao class) in a class which doesn’t extend OrmLiteBaseActivity.
I read the documentation on using with Android and I looked at their no base class example.
As my class isn’t an Activity :
-
it isn’t a Context, so OpenHelperManager.getHelper can’t work.
I know I can get the context from every part of my application using the application but is there a better way ?
-
it doesn’t have a onDestroy to override.
I can put the logic at the end of my function :
if (databaseHelper != null) { OpenHelperManager.releaseHelper(); databaseHelper = null; }But if my class become more complex, what is the good behaviour ?
Regards.
The general rule of thumb is that you should create a single helper object and then release it when all of the parts of your program are done with it. This means that if you have different threads that are using the helper, you’ll need to keep some sort of usage counter — maybe with
AtomicInteger. As each class asks for the helper, the first time it is created but every time afterwards the counter is increased. As they are done with the helper they decrement the counter. When the counter goes to 0 you release it fromOpenHelperManager.You are going to need to get the
Contextsomehow. Using the application should be fine as long as it works.But part of your program does have
onDestroy. You are going to have to application the other parts of your application from the places that do haveonDestroy— maybe by calling a staticdestroy()method.I don’t see any problems with the above even with complex applications. If you’d like maybe setup a register/unregister type of config so as your classes construct they register with some central class which then shuts them down at the end.
Hope some of this helps.