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

StackOverflow https://stackoverflow.com/questions/20635605

  •  18-09-2022
  •  | 
  •  

سؤال

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.

هل كانت مفيدة؟

المحلول

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?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top