Reputation: 53
Using this library "androidx.work:work-runtime-ktx:2.5.0" to run a background thread. But OneTimeWorkRequest is being cancelled everything i exit the screen. What i am trying to acheive is set it run on the application level can someone help please or some hints? here is my code below :
var workManager = WorkManager.getInstance(requireContext())
val data: Data.Builder = Data.Builder().apply {
putLong("inspectionId", id)
}
val task = OneTimeWorkRequest.Builder(NewSyncWorkManager::class.java).apply {
// setConstraints(constraints)
setInputData(data.build())
setInitialDelay(1,TimeUnit.SECONDS)
addTag(id.toString())
}.build()
workManager.enqueue(task)
while NewSyncWorkManager is as following:
class NewSyncWorkManager(context: Context,workerParams: WorkerParameters) :
CoroutineWorker(context, workerParams) {
override suspend fun doWork(): Result {
showNotification()
val foregroundInfo = ForegroundInfo(notificationId, notification!!)
setForeground(foregroundInfo)
if (inspectionId == 0L) {
Log.d("wah", "inspectionId to upload is missing, stopping")
return Result.failure()
}
val propertySections = sectionsRepo.getPropertyLayout(inspectionId)?.reportSections
.....
UPLOAD requests to the server
}
}
Upvotes: 1
Views: 471
Reputation: 53
Fixed the issue by running the UPLOAD requests to the server
inside a GlobalScope
Upvotes: 1