Android:在运行时更新构造函数值和提供程序Dagger2

时间:2018-12-16 17:34:43

标签: java android sqlite dependency-injection dagger-2

我将dagger2库用于我的应用程序。然后我使用android sqlite来存储数据。问题是当我的应用程序位于Runtime中时,可以从webservice / API升级我的数据库版本。据我了解,当我调用DatabaseSqlite db = new DatabaseSqlite (context,version,name_of_database)时可以升级数据库版本。但是,当AppModule's class提供数据库版本时,如何升级数据库版本?我可以在Runtime的Dagger中更新提供程序吗?

这是我的AppModule课:

public class AppModule {

    @Provides
    @Singleton
    Context provideContext(Application application) {
        return application;
    }

    @Provides
    @Singleton
    ApiHelper provideApiHelper(AppApiHelper appApiHelper) {
        return appApiHelper;
    }

    @Provides
    @Singleton
    DataManager provideDataManager(AppDataManager appDataManager) {
        return appDataManager;
    }

    @Provides
    @DatabaseInfo
    String provideDatabaseName() {
        return AppConstants.DB_NAME;
    }

    @Provides
    @DatabaseInfo
    Integer provideDatabaseVersion(PreferencesHelper preferencesHelper) {
        return preferencesHelper.getVersion();
    } // this is how I update Database Version


    @Provides
    @Singleton
    Gson provideGson() {
        return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    }

    @Provides
    @PreferenceInfo
    String providePreferenceName() {
        return AppConstants.PREF_NAME;
    }

    @Provides
    @Singleton
    PreferencesHelper providePreferencesHelper(AppPreferencesHelper appPreferencesHelper) {
        return appPreferencesHelper;
    }

    @Provides
    SchedulerProvider provideSchedulerProvider() {
        return new AppSchedulerProvider();
    }
}

这是我的DatabaseSqlite课:

public class DatabaseSqlite extends SQLiteOpenHelper {
private static final String TAG = "DatabaseSqlite";
private Context context;
private ArrayList<Log_version> mlogVersion = null;

@Inject
public DatabaseSqlite(Context context,
                      @DatabaseInfo String dbName,
                      @DatabaseInfo Integer version) {
    super(context, dbName, null, version);
}

public DatabaseSqlite(Context context, int version, ArrayList<Log_version> version_log) {
    super(context, DB_NAME, null, version);
    this.mlog_version = version_log;
    this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    if (app_version_log != null) {
        for (Log_version dataUpdateModel : Log_version) {
            Log.e(TAG, "onCreate: " + dataUpdateModel.getId());
                db.execSQL(dataUpdateModel.getQuer());
        }
        Log.e(TAG, "onCreate: Finish");

    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < newVersion) {
        onCreate(db);
    }
}

这是我的Datamanager类,负责处理我的DatabaseSqlite类的活动:

public class AppDataManager implements DataManager {

private final ApiHelper mApiHelper;

private final Context mContext;

private final Gson mGson;

private final PreferencesHelper mPreferencesHelper;

private DatabaseSqlite  mDatabaseSqlite;
private Insert mInsert;
private Select mSelect;

@Inject
public AppDataManager(Context context, PreferencesHelper preferencesHelper, ApiHelper apiHelper, Gson gson, DatabaseSqlite databaseSqlite,
                      Insert insert, Select select) {
    mContext = context;
    mPreferencesHelper = preferencesHelper;
    mGson = gson;
    mApiHelper = apiHelper;
    mDatabaseSqlite = databaseSqlite;
    mInsert = insert;
    mSelect = select;

}

@Override
public DatabaseSqlite setDatabaseSqlite(Context context, int Version, ArrayList<Lst_App_version_Log> app_version_log) {
    mDatabaseSqlite = new DatabaseSqlite(context, Version, app_version_log);
    return mDatabaseSqlite;
} // I try to pass data into costructor my Database Sqlite

}

这就是我尝试在“活动”中更新数据库版本的方式:

 getDataManager().setVersion(itemVersionResponse.getVersion());
        getDataManager().setDatabaseSqlite(context, itemVersionResponse.getVersion(), dataUpdateModels);

是否存在某些问题,导致我的数据库版本未升级?请我希望有人能告诉我我的错误在哪里。非常感谢你

0 个答案:

没有答案