简体   繁体   中英

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. 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.

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. Below is MyApp class:

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:

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.

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. 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).

Looks like I was being stupid. In MyApp I needed to add .inject(this), so

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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