简体   繁体   English

在Android应用程序中将数据库连接放在何处

[英]Where to put DB connection in Android Application

I'm having some trouble on where/how to integrate the database connection in my Android application. 我在哪里/如何在我的Android应用程序中集成数据库连接时遇到了一些麻烦。 After trying several different ways I settled on extending the application class and putting it in there. 在尝试了几种不同的方式之后,我决定扩展应用程序类并将其放入其中。 As I understand it though I need it to be static so I can access it from anywhere, and I'm using GreenDAO which requires the Application context to work. 据我了解,尽管我需要它是静态的,所以我可以从任何地方访问它,而且我正在使用GreenDAO,这需要Application上下文才能起作用。

The problem is now I've started using Dagger2 (which I don't really understand but have somehow got it working), but it doesn't allow for static injection. 现在的问题是,我已经开始使用Dagger2(我不太了解,但是以某种方式可以使它工作),但是它不允许进行静态注入。 Below is MyApp class: 以下是MyApp类:

public class MyApp extends Application {

@Inject
public DataSource dataSource;

private static MyApp instance;

public MyApp() {
    instance = this;
}

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

    // Connect to our datasource
    if (dataSource == null) {
         DaggerDataSourceComponent.builder().dataSourceModule(new DataSourceModule(getInstance())).build();
    }
}

public static MyApp getInstance() {
    return instance;
}

public DataSource getDataSource() {
    return dataSource;
}

} }

and here is my DataSource class: 这是我的DataSource类:

public class DataSource {

public DaoSession daoSession;
Context context;
public Database db;
@Inject
public DaoMaster.DevOpenHelper helper;

public DataSource(Context context, DaoMaster.DevOpenHelper helper) {
    this.context = context;
    this.helper = helper;

    db = this.helper.getWritableDb();
    daoSession = new DaoMaster(db).newSession();
}

Generally this all works okay but as soon as I start rotating the screen I get database memory leaks. 通常这一切都可以,但是一旦我开始旋转屏幕,我就会发现数据库内存泄漏。 I think this is caused because the database isn't static, and is being recreated on every rotation? 我认为这是由于数据库不是静态的,并且每次旋转都会重新创建? I can't create the Database connection in my Activity as I understand in MVP the activity should have no knowledge of such things. 我无法在我的活动中创建数据库连接,正如我在MVP中所了解的那样,活动不应该了解此类情况。

Any advice on how I should go about solving these memory leaks would be appreciated. 我应该如何解决这些内存泄漏的任何建议将不胜感激。

UPDATE: 更新:

After playing around it doesn't seem that the injection of the DataSource in MyApp is actually working, it turns out it is only working in my interactor class (which also injects the DataSource), so I didn't realise it was doing nothing in MyApp class. 在玩了之后,似乎似乎没有在MyApp中注入DataSource实际上有效,事实证明,它仅在我的交互器类中起作用(它也注入了DataSource),所以我没有意识到它没有做任何事情。 MyApp类。 Still if possible I'd like to get it sorted in this MyApp class so I can access a single connection globally (unless there is a better way). 仍然,如果可能的话,我还是希望在MyApp类中对它进行排序,以便可以全局访问单个连接(除非有更好的方法)。

Looks like I was being stupid. 看起来我很蠢。 In MyApp I needed to add .inject(this), so 在MyApp中,我需要添加.inject(this),因此

DaggerDataSourceComponent.builder().dataSourceModule(new DataSourceModule(getInstance())).build();

becomes

DaggerDataSourceComponent.builder().dataSourceModule(new DataSourceModule(getInstance())).build().inject(this);

I now have one Database connection that is created and persists even when activities are destroyed, and can be accessed like so throughout the application: 现在,我创建了一个数据库连接,即使活动被破坏,该数据库连接仍然存在,并且可以在整个应用程序中进行访问:

dataSource = MyApp.getInstance().getDataSource();

Not sure if this is good practice, but it solved my problem. 不知道这是否是个好习惯,但这解决了我的问题。 If anyone has any better ideas on how to structure this happy to hear it. 如果有人对如何组织这个有更好的想法,我们很高兴听到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM