公布对@Singleton ORMLite帮手 [英] Releasing ORMLite helper on @Singleton

查看:200
本文介绍了公布对@Singleton ORMLite帮手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在那里我已经注射 OrmLiteSqliteOpenHelper 的一个实例 @Singleton 类。我是不是真的以往任何时候都需要调用 OpenHelperManager.releaseHelper()?万一我做什么,在哪里以及如何要那样作为类不扩展任何Android基类在那里我能得到的的onDestroy

I have a @Singleton class where I've injected an instance of OrmLiteSqliteOpenHelper. Do I actually ever need to call the OpenHelperManager.releaseHelper()? In case I do, where and how should it be done as the class doesn't extend any Android base class where I could get to the onDestroy?

推荐答案

有一个 ORMLite 例如Android项目这表明这家名为 HelloAndroidNoBase 。我会检查出来。

There is an ORMLite example Android project which demonstrates this called HelloAndroidNoBase. I'd check it out.

从主活动相关的code部分包含下面。你需要有这种code在使用数据库的活动或其他类的每一个。

The relevant code section from the main Activity is included below. You'll need to have this sort of code in each one of your Activity or other classes that uses the database.

如果你的类没有一个的onDestroy()方法,那么你需要添加一个,并从其他类中的一个称呼它确实有的onDestroy()。主活动是一个很好的地方。所以,你的 MainActivity.onDestroy()会叫 yourClass.onDestroy()当应用程序被关闭。

If your class does not have an onDestroy() method then you need to add one and call it from one of the other classes that does have onDestroy(). The main Activity is a good place for this. So your MainActivity.onDestroy() would call yourClass.onDestroy() when the application is shutting down.

public class HelloNoBase extends Activity {

    private DatabaseHelper databaseHelper = null;

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

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

这篇关于公布对@Singleton ORMLite帮手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆