如何为工人阶级实施匕首?

Posted

技术标签:

【中文标题】如何为工人阶级实施匕首?【英文标题】:How to implement Dagger for worker classes? 【发布时间】:2018-10-31 21:35:45 【问题描述】:

既然Worker类是由框架创建的(WorkerManager),我们如何在Worker中使用@Inject字段?

【问题讨论】:

github.com/google/dagger/issues/1183 我在提交这个问题之前检查了这个链接。真正的实现并不明显。 @Tuby 看看我的样品github.com/saveendhiman/ComplexRecycler 【参考方案1】:

您必须在要注入的模块中使用@Provides 注释提供类。

首先创建一个包含提供类的模块的组件。

@Component(modules = Module.class)
public interface Component1

    void inject(SyncWorker syncWorker);

模块类

@Module
public class Module

    @Provides
    public ISettingRepository getSettingRepo()
        return new ISettingRepository();
    


现在在您的代码中编写一个构造函数,用于将组件注入您的工作类。

public class SyncWorker extends  Worker 

    @Inject
    ISettingRepository settingRepository;

    public SyncWorker()
        DaggerComponent1.builder().build().inject();
    

    @NonNull
    @Override
    public Result doWork() 

        sync();
        return Result.SUCCESS;
    

    private void sync() 

    

【讨论】:

当我尝试使用 'settingRepository' 时它会抛出 NullPointerException【参考方案2】:

我正在使用these 系列帖子在我的应用程序中实现匕首和ProfileManager 是我在Worker 班级中要参加inject 的班级。

UploadWorker

public class UploadWorker extends Worker 

    @Inject
    ProfileManager profileManager;

    @Inject
    @SuppressWarnings("WeakerAccess")
    public UploadWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) 
    super(context, workerParams);
     Provider.getAppComponent().inject(this);
    

    @NonNull
    @Override
    public Result doWork() 
     profileManager.someMethod();
    


Application 类中,我在 Provider 类中设置 Appcomponent

   @Override
    protected androidInjector<? extends DaggerApplication> applicationInjector() 
        AppComponent appComponent = DaggerAppComponent.builder().application(this).build();
        appComponent.inject(this);
        Provider.setAppComponent(appComponent);
        return appComponent;
    

提供者

public class Provider 

    private static AppComponent appComponent;

    public static AppComponent getAppComponent() 
        return appComponent;
    

    public static void setAppComponent(AppComponent component) 
        appComponent = component;
    


应用组件

@Singleton
@Component(modules = 
        AndroidSupportInjectionModule.class,
        ActivityBuilderModule.class,
        ApiModule.class,
        AppModule.class,
        ViewModelModule.class
)
public interface AppComponent extends AndroidInjector<DaggerApplication> 

    @Override
    void inject(DaggerApplication instance);

    void inject(MyApplication app);

    void inject(UploadWorker uploadWorker);

    @Component.Builder
    interface Builder 
        @BindsInstance
        Builder application(Application application);

        AppComponent build();
    


【讨论】:

当我们启动worker的时候,它里面没有构造函数,你是怎么用这个的? 你为什么不创建一个? 我看到它被父级覆盖了。此解决方案有效,但您是否看到“gist.github.com/ThePredators/1702e79c5d3860f415c542da446420eb”它有不同的方法和大量工作,但对我不起作用。您的解决方案与内存有关的任何缺点? 抱歉不熟悉这个解决方案,我认为我的解决方案没有任何缺点。

以上是关于如何为工人阶级实施匕首?的主要内容,如果未能解决你的问题,请参考以下文章

iOS:当您没有权利文件时,如何为您的应用程序实施关键警报?

如何为 xgboost 实施增量训练?

如何为 Jenkins 管道中的失败阶段实施重试选项?

如何为 FTP/HTTP 实施客户端带宽限制?

如何为特定用户实施旧密码和新密码?

如何为未知目的地实施 Google Directions API?