In one of my android(with Kotlin) app I want to use WorkManager class as a generic one. This is my class where I want to use it as generic by passing expected params:
class CommonWorkManager<P, R> (appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams)
{
var lambdaFunction: ((P) -> R)? = null
override fun doWork(): Result {
lambdaFunction
return Result.SUCCESS
}
}
This is how I am trying to create an instance of this class:
CommonWorkManager<Unit, Unit>(context!!, ).lambdaFunction= {
presenter?.fetchMasterData()
}
So How can I pass workerParams
as a second param.
Here 'P' is Parameter and 'R' is Return type in CommonWorkManager<P, R>
答案 0 :(得分:0)
似乎我们无法创建WorkerParameters
的实例,因为它具有带注释@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
的隐藏构造函数。
根据{{3}},我们没有创建Worker
子类的实例,该库为我们做到了:
首先,您将定义
Worker
类,并覆盖其doWork()
方法。您的工作程序类指定了执行操作的方式,但是没有有关任务何时运行的任何信息。接下来,您基于该OneTimeWorkRequest
创建一个Worker
对象,然后使用WorkManager
使该任务排队:
val work = OneTimeWorkRequest.Builder(CommonWorkManager::class.java).build()
WorkManager.getInstance().enqueue(work)
我们可以得出结论,我们无法创建通用的Worker
,即您所创建的CommonWorkManager<P, R>
。 WorkManager
用于特定任务。