How to cancel Work from WorkManager Android?












1














I have saved the work UUID converted to String in Realm.



Here is the code -



Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
Data inputData = new Data.Builder().putString("downloadUrl", downloadUrl).
putString("destinationFolder", destinationFolder).
putInt("suraNumber", Integer.parseInt(suraNumber)).
putString("fileName", fileName).
putBoolean("downloadFileTypeBangla", downloadFileTypeBangla).
putBoolean("downloadFileTypeArabic", downloadFileTypeArabic).
putBoolean("downloadFileTypeArabicWithBangla", downloadFileTypeArabicWithBangla).build();
OneTimeWorkRequest downloadWork = new OneTimeWorkRequest.Builder(DownloadWorker.class).setConstraints(constraints).setInputData(inputData).build();
WorkManager.getInstance().enqueue(downloadWork);

Sura sura = dbOperations.getSuraById(Integer.parseInt(suraNumber));
if(sura != null){
dbOperations.updateSura(sura, Integer.parseInt(suraNumber), sura.getBnAudioDownloadStatus(), sura.getArAudioDownloadStatus(), 1);
realm.beginTransaction();
DownloadStatusModel downloadStatusModel = new DownloadStatusModel();
downloadStatusModel.setId(new RealmCommonService(realm).newId(DownloadStatusModel.class));
downloadStatusModel.setDownloadFileType("ArabicWithBangla");
downloadStatusModel.setActiveStatus(true);
downloadStatusModel.setDownloadDate(new Date());
downloadStatusModel.setDownloadedSuraNo(sura.getSuraNo());
downloadStatusModel.setDownloadFileSize(sura.getArBnAudioFileSize());
downloadStatusModel.setDownloadReferenceId(downloadWork.getId().toString());
downloadStatusModel.setDownloadedSuraNameBangla(sura.getSuraNameBangla());
downloadStatusModel.setDownloadStatus(1);
realm.copyToRealm(downloadStatusModel);
realm.commitTransaction();
}


Now I'm trying to cancel the Work using this line of code but didn't work.



WorkManager.getInstance().cancelWorkById(UUID.fromString(downloadStatusModel.getDownloadReferenceId()));


Any help would be appreciated



Thanks










share|improve this question






















  • Try cancelling work by tag cancelAllWorkByTag and provide same tag to your worker. But remember that it works if your work is unfinished.
    – Jeel Vankhede
    Nov 23 '18 at 5:42












  • Thanks for your comment. Will it cancel my all works that I have enqueued?
    – Al-Amin
    Nov 23 '18 at 5:44










  • Yes !, it would. May be your issue is not with UUID but it's already finished (Just guessing).
    – Jeel Vankhede
    Nov 23 '18 at 5:45










  • No luck. cancelAllWorkByTag not working. My work is not finished also. I have debugged that.
    – Al-Amin
    Nov 23 '18 at 6:31






  • 1




    What do you mean 'it didn't work'? How are you checking that it wasn't cancelled? Does your Worker get a callback to onStopped()?
    – ianhanniballake
    Nov 23 '18 at 8:20
















1














I have saved the work UUID converted to String in Realm.



Here is the code -



Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
Data inputData = new Data.Builder().putString("downloadUrl", downloadUrl).
putString("destinationFolder", destinationFolder).
putInt("suraNumber", Integer.parseInt(suraNumber)).
putString("fileName", fileName).
putBoolean("downloadFileTypeBangla", downloadFileTypeBangla).
putBoolean("downloadFileTypeArabic", downloadFileTypeArabic).
putBoolean("downloadFileTypeArabicWithBangla", downloadFileTypeArabicWithBangla).build();
OneTimeWorkRequest downloadWork = new OneTimeWorkRequest.Builder(DownloadWorker.class).setConstraints(constraints).setInputData(inputData).build();
WorkManager.getInstance().enqueue(downloadWork);

Sura sura = dbOperations.getSuraById(Integer.parseInt(suraNumber));
if(sura != null){
dbOperations.updateSura(sura, Integer.parseInt(suraNumber), sura.getBnAudioDownloadStatus(), sura.getArAudioDownloadStatus(), 1);
realm.beginTransaction();
DownloadStatusModel downloadStatusModel = new DownloadStatusModel();
downloadStatusModel.setId(new RealmCommonService(realm).newId(DownloadStatusModel.class));
downloadStatusModel.setDownloadFileType("ArabicWithBangla");
downloadStatusModel.setActiveStatus(true);
downloadStatusModel.setDownloadDate(new Date());
downloadStatusModel.setDownloadedSuraNo(sura.getSuraNo());
downloadStatusModel.setDownloadFileSize(sura.getArBnAudioFileSize());
downloadStatusModel.setDownloadReferenceId(downloadWork.getId().toString());
downloadStatusModel.setDownloadedSuraNameBangla(sura.getSuraNameBangla());
downloadStatusModel.setDownloadStatus(1);
realm.copyToRealm(downloadStatusModel);
realm.commitTransaction();
}


Now I'm trying to cancel the Work using this line of code but didn't work.



WorkManager.getInstance().cancelWorkById(UUID.fromString(downloadStatusModel.getDownloadReferenceId()));


Any help would be appreciated



Thanks










share|improve this question






















  • Try cancelling work by tag cancelAllWorkByTag and provide same tag to your worker. But remember that it works if your work is unfinished.
    – Jeel Vankhede
    Nov 23 '18 at 5:42












  • Thanks for your comment. Will it cancel my all works that I have enqueued?
    – Al-Amin
    Nov 23 '18 at 5:44










  • Yes !, it would. May be your issue is not with UUID but it's already finished (Just guessing).
    – Jeel Vankhede
    Nov 23 '18 at 5:45










  • No luck. cancelAllWorkByTag not working. My work is not finished also. I have debugged that.
    – Al-Amin
    Nov 23 '18 at 6:31






  • 1




    What do you mean 'it didn't work'? How are you checking that it wasn't cancelled? Does your Worker get a callback to onStopped()?
    – ianhanniballake
    Nov 23 '18 at 8:20














1












1








1







I have saved the work UUID converted to String in Realm.



Here is the code -



Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
Data inputData = new Data.Builder().putString("downloadUrl", downloadUrl).
putString("destinationFolder", destinationFolder).
putInt("suraNumber", Integer.parseInt(suraNumber)).
putString("fileName", fileName).
putBoolean("downloadFileTypeBangla", downloadFileTypeBangla).
putBoolean("downloadFileTypeArabic", downloadFileTypeArabic).
putBoolean("downloadFileTypeArabicWithBangla", downloadFileTypeArabicWithBangla).build();
OneTimeWorkRequest downloadWork = new OneTimeWorkRequest.Builder(DownloadWorker.class).setConstraints(constraints).setInputData(inputData).build();
WorkManager.getInstance().enqueue(downloadWork);

Sura sura = dbOperations.getSuraById(Integer.parseInt(suraNumber));
if(sura != null){
dbOperations.updateSura(sura, Integer.parseInt(suraNumber), sura.getBnAudioDownloadStatus(), sura.getArAudioDownloadStatus(), 1);
realm.beginTransaction();
DownloadStatusModel downloadStatusModel = new DownloadStatusModel();
downloadStatusModel.setId(new RealmCommonService(realm).newId(DownloadStatusModel.class));
downloadStatusModel.setDownloadFileType("ArabicWithBangla");
downloadStatusModel.setActiveStatus(true);
downloadStatusModel.setDownloadDate(new Date());
downloadStatusModel.setDownloadedSuraNo(sura.getSuraNo());
downloadStatusModel.setDownloadFileSize(sura.getArBnAudioFileSize());
downloadStatusModel.setDownloadReferenceId(downloadWork.getId().toString());
downloadStatusModel.setDownloadedSuraNameBangla(sura.getSuraNameBangla());
downloadStatusModel.setDownloadStatus(1);
realm.copyToRealm(downloadStatusModel);
realm.commitTransaction();
}


Now I'm trying to cancel the Work using this line of code but didn't work.



WorkManager.getInstance().cancelWorkById(UUID.fromString(downloadStatusModel.getDownloadReferenceId()));


Any help would be appreciated



Thanks










share|improve this question













I have saved the work UUID converted to String in Realm.



Here is the code -



Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
Data inputData = new Data.Builder().putString("downloadUrl", downloadUrl).
putString("destinationFolder", destinationFolder).
putInt("suraNumber", Integer.parseInt(suraNumber)).
putString("fileName", fileName).
putBoolean("downloadFileTypeBangla", downloadFileTypeBangla).
putBoolean("downloadFileTypeArabic", downloadFileTypeArabic).
putBoolean("downloadFileTypeArabicWithBangla", downloadFileTypeArabicWithBangla).build();
OneTimeWorkRequest downloadWork = new OneTimeWorkRequest.Builder(DownloadWorker.class).setConstraints(constraints).setInputData(inputData).build();
WorkManager.getInstance().enqueue(downloadWork);

Sura sura = dbOperations.getSuraById(Integer.parseInt(suraNumber));
if(sura != null){
dbOperations.updateSura(sura, Integer.parseInt(suraNumber), sura.getBnAudioDownloadStatus(), sura.getArAudioDownloadStatus(), 1);
realm.beginTransaction();
DownloadStatusModel downloadStatusModel = new DownloadStatusModel();
downloadStatusModel.setId(new RealmCommonService(realm).newId(DownloadStatusModel.class));
downloadStatusModel.setDownloadFileType("ArabicWithBangla");
downloadStatusModel.setActiveStatus(true);
downloadStatusModel.setDownloadDate(new Date());
downloadStatusModel.setDownloadedSuraNo(sura.getSuraNo());
downloadStatusModel.setDownloadFileSize(sura.getArBnAudioFileSize());
downloadStatusModel.setDownloadReferenceId(downloadWork.getId().toString());
downloadStatusModel.setDownloadedSuraNameBangla(sura.getSuraNameBangla());
downloadStatusModel.setDownloadStatus(1);
realm.copyToRealm(downloadStatusModel);
realm.commitTransaction();
}


Now I'm trying to cancel the Work using this line of code but didn't work.



WorkManager.getInstance().cancelWorkById(UUID.fromString(downloadStatusModel.getDownloadReferenceId()));


Any help would be appreciated



Thanks







android realm android-workmanager android-architecture-workmanager






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 5:34









Al-Amin

422313




422313












  • Try cancelling work by tag cancelAllWorkByTag and provide same tag to your worker. But remember that it works if your work is unfinished.
    – Jeel Vankhede
    Nov 23 '18 at 5:42












  • Thanks for your comment. Will it cancel my all works that I have enqueued?
    – Al-Amin
    Nov 23 '18 at 5:44










  • Yes !, it would. May be your issue is not with UUID but it's already finished (Just guessing).
    – Jeel Vankhede
    Nov 23 '18 at 5:45










  • No luck. cancelAllWorkByTag not working. My work is not finished also. I have debugged that.
    – Al-Amin
    Nov 23 '18 at 6:31






  • 1




    What do you mean 'it didn't work'? How are you checking that it wasn't cancelled? Does your Worker get a callback to onStopped()?
    – ianhanniballake
    Nov 23 '18 at 8:20


















  • Try cancelling work by tag cancelAllWorkByTag and provide same tag to your worker. But remember that it works if your work is unfinished.
    – Jeel Vankhede
    Nov 23 '18 at 5:42












  • Thanks for your comment. Will it cancel my all works that I have enqueued?
    – Al-Amin
    Nov 23 '18 at 5:44










  • Yes !, it would. May be your issue is not with UUID but it's already finished (Just guessing).
    – Jeel Vankhede
    Nov 23 '18 at 5:45










  • No luck. cancelAllWorkByTag not working. My work is not finished also. I have debugged that.
    – Al-Amin
    Nov 23 '18 at 6:31






  • 1




    What do you mean 'it didn't work'? How are you checking that it wasn't cancelled? Does your Worker get a callback to onStopped()?
    – ianhanniballake
    Nov 23 '18 at 8:20
















Try cancelling work by tag cancelAllWorkByTag and provide same tag to your worker. But remember that it works if your work is unfinished.
– Jeel Vankhede
Nov 23 '18 at 5:42






Try cancelling work by tag cancelAllWorkByTag and provide same tag to your worker. But remember that it works if your work is unfinished.
– Jeel Vankhede
Nov 23 '18 at 5:42














Thanks for your comment. Will it cancel my all works that I have enqueued?
– Al-Amin
Nov 23 '18 at 5:44




Thanks for your comment. Will it cancel my all works that I have enqueued?
– Al-Amin
Nov 23 '18 at 5:44












Yes !, it would. May be your issue is not with UUID but it's already finished (Just guessing).
– Jeel Vankhede
Nov 23 '18 at 5:45




Yes !, it would. May be your issue is not with UUID but it's already finished (Just guessing).
– Jeel Vankhede
Nov 23 '18 at 5:45












No luck. cancelAllWorkByTag not working. My work is not finished also. I have debugged that.
– Al-Amin
Nov 23 '18 at 6:31




No luck. cancelAllWorkByTag not working. My work is not finished also. I have debugged that.
– Al-Amin
Nov 23 '18 at 6:31




1




1




What do you mean 'it didn't work'? How are you checking that it wasn't cancelled? Does your Worker get a callback to onStopped()?
– ianhanniballake
Nov 23 '18 at 8:20




What do you mean 'it didn't work'? How are you checking that it wasn't cancelled? Does your Worker get a callback to onStopped()?
– ianhanniballake
Nov 23 '18 at 8:20












1 Answer
1






active

oldest

votes


















1














If you're using Worker, you need to override the onStopped() method and use that as the signal for your worker to cancel its ongoing work. Within your doWork() method, you can also use isStopped() to check for cancellation.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441122%2fhow-to-cancel-work-from-workmanager-android%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If you're using Worker, you need to override the onStopped() method and use that as the signal for your worker to cancel its ongoing work. Within your doWork() method, you can also use isStopped() to check for cancellation.






    share|improve this answer


























      1














      If you're using Worker, you need to override the onStopped() method and use that as the signal for your worker to cancel its ongoing work. Within your doWork() method, you can also use isStopped() to check for cancellation.






      share|improve this answer
























        1












        1








        1






        If you're using Worker, you need to override the onStopped() method and use that as the signal for your worker to cancel its ongoing work. Within your doWork() method, you can also use isStopped() to check for cancellation.






        share|improve this answer












        If you're using Worker, you need to override the onStopped() method and use that as the signal for your worker to cancel its ongoing work. Within your doWork() method, you can also use isStopped() to check for cancellation.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 8:33









        ianhanniballake

        103k14214227




        103k14214227






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441122%2fhow-to-cancel-work-from-workmanager-android%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Sphinx de Gizeh

            Dijon

            Guerrita