Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not binding the OrmLiteSqliteOpenHelper to the Application instead of Activity?

In the ORMLite documentation it is recommended to create the OrmLiteSqliteOpenHelper for every activity. As getting the helper only needs a Context object why not creating the database helper once for the whole Application object? This would open the possibility to have the database helper being injected using Dagger (or other injection frameworks) into classes that need it.

Is there any reason not to create one single application-wide database helper? Is it preferable to have every single activity having its own database helper? I can imagine this keeps the helper's cache size small as the cache contains only objects related to its activity.

like image 373
Lars Blumberg Avatar asked Oct 03 '22 03:10

Lars Blumberg


1 Answers

In the ORMLite documentation it is recommended to create the OrmLiteSqliteOpenHelper for every activity.

This should not be implying that you need a separate instance of the helper for each activity. You certainly can/should share the helper throughout your application.

To quote from the docs:

We recommend using the OpenHelperManager to monitor the usage of the helper – it will create it on the first access, track each time a part of your code is using it, and then it will close the last time the helper is released.

The OpenHelperManager creates and manages the helper singleton.

If you point me to the point in the docs that indicates that you need a different one per activity and I'll tweak them?

like image 87
Gray Avatar answered Oct 13 '22 10:10

Gray