Multithreading OrmLite

I need to have access to the assistant not only from Activity, but, for example, from BroadcastReceivers and AsyncTasks. Is it correct that if I use OrmLiteBaseActivity for the approach, then use the following methods:

OpenHelperManager.getHelper(context, DatabaseHelper.class); OpenHelperManager.releaseHelper(); 

inside not activity classes?

EDIT:

I understand that the helper life cycle is handled by OrmLiteBaseActivity . I ask how to handle the assistant's life cycle outside of the activity. For example, I need access to a database from BroadcastReceiver or AsyncTask . This is the right way to achieve this using OpenHelperManager.getHelper(context, DatabaseHelper.class); when I run the database stuff in another thread and OpenHelperManager.releaseHelper(); when I did all the work with the database and want to free the assistant?

+4
source share
1 answer

I am right that if I use OrmLiteBaseActivity for an approach, then use these methods ...

Yes, using the OpenHelperManager.getHelper(...) and releaseHelper() methods is the right way to do this. To specify from ORMLite Android docs :

If you do not want to extend OrmLiteBaseActivity and other base classes, you will need to duplicate their functionality. You will need to call OpenHelperManager.getHelper(Context context, Class openHelperClass) at the beginning of your code, save the helper and use it as much as you want, and then call OpenHelperManager.release() when you are done with it. You probably want to have something like the following in your classes:

Example code in documents:

 private DatabaseHelper databaseHelper = null; @Override protected void onDestroy() { super.onDestroy(); if (databaseHelper != null) { OpenHelperManager.releaseHelper(); databaseHelper = null; } } private DBHelper getHelper() { if (databaseHelper == null) { databaseHelper = OpenHelperManager.getHelper(this, DatabaseHelper.class); } return databaseHelper; } 
+5
source

Source: https://habr.com/ru/post/1493952/