Workmanager jobs stop working or do not complete on a third or fourth attempt

14 views Asked by At

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.

0

There are 0 answers