Workmanager jobs stop working or do not complete on a third
2024-03-26 02:47

my English is not very good so forgive me, I have been having a small problem for days, the works I do with WorkManager I use to store data in the background and when the task is completed the application notifies me. The problem is that the fourth or third time you run the job it stops working, you can stop it manually, but it never completes. I attach my code that guided me to create, execute and see the status of the Work.

private void sincronizar() { Log.d("Message ::","Sincronizando"); manager = WorkManager.getInstance(getActivity()); manager.enqueueUniqueWork("startW",ExistingWorkPolicy.KEEP,startWork); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } viewModel.setLoading(true); viewModel.setRegistroSync(tag); int sum = 0; if(cbCobros.isChecked()) { manager.enqueueUniqueWork("cobroW", ExistingWorkPolicy.KEEP,cobroWork); sum++; } if(cbPedidos.isChecked()) { manager.enqueueUniqueWork("pedidoW", ExistingWorkPolicy.KEEP,pedidoWork); sum++; } if (cbEmpresas.isChecked()) { manager.enqueueUniqueWork("empresaW", ExistingWorkPolicy.KEEP,empresaWork); sum++; } if(cbMonedas.isChecked()) { manager.enqueueUniqueWork("monedaW", ExistingWorkPolicy.KEEP,monedaWork); sum++; } if(cbDocumentos.isChecked()) { manager.enqueueUniqueWork("documentoW", ExistingWorkPolicy.KEEP,documentoWork); sum++; } if(cbPerfil.isChecked()) { manager.enqueueUniqueWork("perfilW", ExistingWorkPolicy.KEEP,perfilWork); sum++; } if(cbCatalogo.isChecked()) { manager.enqueueUniqueWork("catalogoW", ExistingWorkPolicy.KEEP,catalogoWork); sum++; } if(cbDatosempresa.isChecked()) { manager.enqueueUniqueWork("datosW", ExistingWorkPolicy.KEEP,datosWork); sum++; } if(cbConfig.isChecked()) { manager.enqueueUniqueWork("configW", ExistingWorkPolicy.KEEP,configWork); sum++; } if(cbVisitas.isChecked()) { manager.enqueueUniqueWork("visitaW", ExistingWorkPolicy.KEEP,visitaWorker); sum++; } if(cbCotizaciones.isChecked()) { manager.enqueueUniqueWork("cotizacionW", ExistingWorkPolicy.KEEP,cotizacionWork); sum++; } if(cbSurtir.isChecked()) { manager.enqueueUniqueWork("surtirW", ExistingWorkPolicy.KEEP,surtirWork); sum++; } if(cbEntrega.isChecked()) { manager.enqueueUniqueWork("entregaW", ExistingWorkPolicy.KEEP,entregaWork); sum++; } if(cbFacturas.isChecked()) { manager.enqueueUniqueWork("facturaW", ExistingWorkPolicy.KEEP,facturaWork); sum++; } if(cbClientes.isChecked()) { manager.enqueueUniqueWork("clienteW", ExistingWorkPolicy.KEEP,clientWork); sum++; } if(cbArticulo.isChecked()) { manager.enqueueUniqueWork("articuloW", ExistingWorkPolicy.KEEP,articuloWork); sum++; } if(cbFormasPago.isChecked()) { manager.enqueueUniqueWork("formasPagoW", ExistingWorkPolicy.KEEP,formasPagoWork); sum++; } if(cbVentas.isChecked()) { manager.enqueueUniqueWork("ventasW", ExistingWorkPolicy.KEEP,ventasWork); sum++; } if(cbCajas.isChecked()) { manager.enqueueUniqueWork("cajasW", ExistingWorkPolicy.KEEP,cajasWork); sum++; } tvProgress.setText("Sincronizando..."); if(sum == 0) { viewModel.setLoading(false); viewModel.updateRegistroSync(tag); } else{ tvCancelar.setVisibility(View.VISIBLE); } AtomicInteger sumW = new AtomicInteger(); manager.getWorkInfosByTagLiveData(tag) .observe(getViewLifecycleOwner(), workInfos -> { if (workInfos != null) { int count = workInfos.size(); sumW.set(0); workInfos.forEach(workInfo -> { Log.d("Message :::", "Work: " + workInfo.getTags() + ", State: " + workInfo.getState()); if(workInfo.getState() == WorkInfo.State.SUCCEEDED) { sumW.getAndIncrement(); } Log.d("Message :::","sum: " + sumW.intValue() + ", count: " + count); if(sumW.intValue() == count) { getInfoRegistered(); viewModel.setLoading(false); tvProgress.setText("Sincronizaci¨Žn completa"); manager.enqueue(finishWork); WorkerUtils.makeSuccessNotification("Sincronizaci¨Žn completa", getActivity()); manager.pruneWork(); } }); workInfos.stream().forEach(workInfo -> { Data progress = workInfo.getProgress(); int value = progress.getInt("PROGRESS", 0); if(value > 0) { pbLoading.setVisibility(View.VISIBLE); pbLoading.setProgress((int) value,true); } } ); } }); }

Any errors in my logic please let me know, I am somewhat new to Android development.

I already tried to purge it with the manager.pruneWork() command but this error continues, with or without the command in place.




other answer :

From the provided code, it seems you are enqueuing multiple unique works using enqueueUniqueWork method. Each work is given a unique name to identify it. However, youre specifying ExistingWorkPolicy.KEEP, which means if a work with the same unique name already exists, the new work wont be enqueued.

The issue youre facing might occur because the existing works might not be completing successfully, causing subsequent works with the same unique name to not be enqueued.

Here are some steps you can take to diagnose and potentially fix the issue:

Check WorkManager Logs: WorkManager provides logs that can help diagnose issues. Check the logs to see if there are any errors or warnings related to the works that are not completing.

Update Existing Work Policies: Instead of ExistingWorkPolicy.KEEP, you might consider using ExistingWorkPolicy.REPLACE or ExistingWorkPolicy.APPEND depending on your use case. REPLACE will cancel the existing work and replace it with the new one, while APPEND will add the new work to the existing chain.

Implement WorkManager Callbacks: Implement WorkManager callbacks such as WorkManager.enqueue() and WorkManager.getWorkInfoByIdLiveData() to monitor the status of your works. You can observe the work status and take appropriate actions based on the result.

Check WorkManager Constraints: Ensure that your works are not failing due to any constraints specified, such as network availability or battery conditions. If any constraints are failing, the work wont be executed.

Handle Work Cancellation: Make sure your works are not being cancelled inadvertently due to system conditions or user actions. If a work is cancelled, it wont complete, and subsequent works might not be enqueued.

Review Work Execution Logic: Review the logic inside your worker classes to ensure there are no errors or exceptions that might cause the works to fail or hang.

By reviewing these aspects and implementing proper error handling and monitoring mechanisms, you should be able to diagnose and fix the issue with your WorkManager jobs not completing successfully.