它是确定以创建应用程序类ORMLite数据库帮手?它是、帮手、应用程序、数据库

2023-09-05 05:51:23 作者:夏末

我想创建使用ORMLite包一个Android应用程序。我有几个acivities和服务,也可以使用https://github.com/tomquist/Android-Error-Reporter能够接收来自客户的掌上电脑的错误。 ORMLite需要所有活动和服务扩展OrmLiteBaseActivity等,或添加适当的code到每一个活动能够得到数据库的辅助和活动结束后,将其释放。所以这不是很方便的添加这个code到每一个活动或服务。我也有一些可以使用数据库辅助类

i am trying to create an android application using ORMLite package. I have a few acivities and services and also use https://github.com/tomquist/Android-Error-Reporter to be able to receive errors from clients' pdas. ORMLite requires that all activities and services extend OrmLiteBaseActivity etc or add appropriate code to each activity to be able to get database helper and release it after the activity is finished. so this isn't very convenient to add this code to every activity or service. i also have some helper classes which can use database

我也有拥有一些全球性的信息和方法的应用程序类。所以我决定在应用程序类开放ormlite帮手,并通过所有性活动中使用它/类这样的:

i also have an application class that holds some global information and methods. so i decided to open ormlite helper in application class and use it through all activities/classes like this:

public class MyApplication extends Application {
  private volatile DatabaseHelper databaseHelper = null;

  @Override
  public void onCreate() {
    super.onCreate();
  }

  @Override
  public void onTerminate() {
    if (databaseHelper != null) {
      OpenHelperManager.releaseHelper();
      databaseHelper = null;
    }
    super.onTerminate();
  }

  public DatabaseHelper getHelper() {
    if (databaseHelper == null) {
      databaseHelper = OpenHelperManager.getHelper(this, DatabaseHelper.class);
    }
    return databaseHelper;
  }
}

和使用它在其他类这样的:

and use it in other classes this way:

((MyApplication) getApplicationContext()).getHelper();

你认为它就是用这种方式还是可以有一些内存泄漏或使用该其他问题是一个好主意?我担心的是,onTerminate永远工作在实际设备...我对尝试新的东西了,所以想听听这方面有任何意见阶段,以消除问题,我可以在未来得到一个错误的做法,并没有改写code。

do you think it is a good idea to use it this way or there can be some memory leaks or other problems with this? my concern is that onTerminate never works on real devices... i am on the stage of "trying new stuff out" so would like to hear any comments about this to eliminate problems i can get in the future with a wrong approach and not having to rewrite the code.

顺便说一句,我是很新的Java和Android和做不时因为我必须这样做,而且喜欢学习新的东西 - 以防万一的问题可能看起来愚蠢或什么的。

btw, i am very new to java and android and do it from time to time cause i have to do it and like to learn new stuff - just in case the question may seem silly or something.

感谢你在前进

推荐答案

整体机制看起来不错@Alex但据我所知, onTerminate()只用在emulated环境所以没有多大用处。你的程序得到由Android操作系统杀害,当它终止在真实设备上,所以没有理由担心内存泄漏和这样的。

The overall mechanism looks fine @Alex but as far as I know, onTerminate() is only used in emulated environments so doesn't have much use. You program gets killed by the Android OS when it terminates on a real device so there is no reason to be worried about memory leaks and the such.

什么是您的code最重要的是,它保证了一个 databaseHelper 实例。每个实例有它自己的数据库连接和问题发生时,有一个以上的(1)连接在一程序中打开到数据库。 sqlite的处理使用在同一时间同一个连接的多个线程,但它不处理多个连接好,可能会出现数据不一致。

What is most important with your code is that it guarantees a single databaseHelper instance. Each instance has it's own connection to the database and problems happen when there are more than one (1) connection opened to the database in a program. Sqlite handles multiple threads using the same connection at the same time but it doesn't handle multiple connections well and data inconsistencies may occur.