Android 匕首2-构造函数注入-非活动
为了解决一个特定的问题,我刚刚开始学习Dagger 2:我试图遵循MVVM体系结构,我的应用程序有一个repository类,它在一个基本上包装SharedReference的CacheData类中提取和保存设置。但是,SharedReferences具有上下文依赖性。由于我已经完成了将存储库和数据层与视图和应用程序类分离的所有工作,因此传递上下文似乎是一种倒退 这是存储库类Android 匕首2-构造函数注入-非活动,android,mvvm,repository,sharedpreferences,dagger-2,Android,Mvvm,Repository,Sharedpreferences,Dagger 2,为了解决一个特定的问题,我刚刚开始学习Dagger 2:我试图遵循MVVM体系结构,我的应用程序有一个repository类,它在一个基本上包装SharedReference的CacheData类中提取和保存设置。但是,SharedReferences具有上下文依赖性。由于我已经完成了将存储库和数据层与视图和应用程序类分离的所有工作,因此传递上下文似乎是一种倒退 这是存储库类 public class ImgurRepository { private ImgurDatabase img
public class ImgurRepository {
private ImgurDatabase imgurDatabase;
private ImgurGalleryDao galleryDao;
private CompositeDisposable disposables;
private LiveData<List<ImgurGallery>> imgurGalleries;
public ImgurRepository(){
this.imgurDatabase = ImgurDatabase.getInstance(MyRxApplication.getAppContext());
this.galleryDao = imgurDatabase.getGalleryDao();
disposables = new CompositeDisposable();
}
public String getCachedSearchTerm() {
CachedData cachedData = new CachedData();
return cachedData.getCachedSearchTerm();
}
public String getCachedSearchWindow(){
CachedData cachedData = new CachedData();
return cachedData.getCachedSearchWindow();
}
public String getCachedSearchType(){
CachedData cachedData = new CachedData();
return cachedData.getCachedSearchType();
}
public void setCachedSearchParams(@NonNull final String term,
@NonNull final String type,
@NonNull final String window) {
CachedData cachedData = new CachedData();
cachedData.setCachedSearchParams(term, type, window);
}
public LiveData<List<ImgurGallery>> getCachedGalleries() {
return this.imgurGalleries;
}
public LiveData<List<ImgurGallery>> fetchGalleries(@NonNull final String searchType,
@NonNull final String searchWindow,
@NonNull final String searchTerm,
final int resultsPage){
requestGalleries(searchType, searchWindow, searchTerm, resultsPage);
return galleryDao.getAll();
}
private void requestGalleries(@NonNull final String searchType,
@NonNull final String searchWindow,
@NonNull final String searchTerm,
final int resultsPage) {
Timber.d("Running fetchGalleries with arguments:\nsort='%s' \nwindow='%s'\nsearch='%s'\npage='%s'",
searchType,
searchWindow,
searchTerm,
resultsPage);
ServiceGenerator.changeApiBaseUrl(IMGUR_API_BASE_URL);
ImgurService service = ServiceGenerator.createService(ImgurService.class);
Timber.d("finishing fetchGalleries request.");
disposables.add(service.getSearchGallery(searchType,searchWindow,resultsPage,searchTerm)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Response<ImgurGalleryList>>() {
@Override
public void accept(@NonNull final Response<ImgurGalleryList> response) throws Exception {
Timber.d("Consumer is subscribed to imgurGalleryObservable.");
Timber.d(response.body().toString());
List<ImgurGallery> galleries = response.body().getData();
galleryDao.insertAll(galleries);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Timber.e(throwable);
}
}));
}
public void clearGalleries() {
galleryDao.deleteAll();
}
}
固定AppComponent类:
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
void inject(MyRxApplication app);
void inject(CachedData cachedData);
void inject(BaseActivity activity);
}
AppModule类:
@Module
public class AppModule {
private final String SHARED_PREFERENCE_KEY = "PREFERENCE_FILE_KEY";
private final MyRxApplication application;
public AppModule(MyRxApplication application) {
this.application = application;
}
@Provides
@Singleton
public Context provideApplicationContext(){
return application;
}
@Provides
@Singleton
public SharedPreferences provideSharedPreferences() {
return application
.getSharedPreferences(
SHARED_PREFERENCE_KEY,
Context.MODE_PRIVATE);
}
}
应用程序类别:
public class MyRxApplication extends Application {
private static Context context;
private AppComponent appComponent;
public MyRxApplication() {
}
@Override
public void onCreate() {
super.onCreate();
MyRxApplication.context = getApplicationContext();
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
LeakCanary.install(this);
Stetho.initializeWithDefaults(this);
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
}
appComponent = buildComponent();
appComponent.inject(this);
appComponent.cachedData();
}
public AppComponent buildComponent(){
return DaggerAppComponent
.builder()
.appModule(new AppModule(this))
.build();
}
public AppComponent getAppComponent() {
return appComponent;
}
public static Context getAppContext() {
return MyRxApplication.context;
}
}
是否可以使用Dagger将SharedReferences对象注入CachedData构造函数?我正在构造许多CachedData对象,因为我最初不想使用单例,但是将CachedData对象作为单例注入有意义吗?我真的不确定现在使用Dagger的正确方法,并让它工作,因为我在CacheData构造函数中设置的每个参数都需要在我创建它时提供,即使我认为我正在使用Dagger来完成 据我所知,从您的附加命令来看,您不希望对类
CachedData
使用注入,因此希望手动实例化它。如果是这种情况,您可以删除您注入的构造函数参数,并直接注入变量,如下所示:
@Inject
SharedPreferences sharedPrefs;
@Inject
Context context;
@Inject
public CachedData() {}
是否可以使用Dagger将SharedReferences对象注入CachedData构造函数?我不理解您的问题,您已经在代码中这样做了。@SamuelEminet我想我的问题是,没有明确的构造函数注入的Exmaple,我不太理解如何使用它。我有一个CachedData类,在构造函数中包含SharedReference和上下文参数。我想使用Dagger注入这些对象,但在我的存储库中创建CacheData对象的同时,它需要我传递SharedPref和Context。这是违反直觉的,我一定是做错了,但是很难找到一个构造函数注入的例子来清楚说明在这种情况下如何使用Dagger。根据您的评论更改了CachedData类和AppComponent类。现在在CachedData中的SharedReferences上接收到一个nullpointer异常,但是模块SharedRefers不是null,这意味着它没有被注入SharedRefers。抱歉
@Inject
在复制/粘贴期间从构造函数中移除
@Inject
SharedPreferences sharedPrefs;
@Inject
Context context;
@Inject
public CachedData() {}