Cancel task in Grand Central Dispatch GCD with DispatchWorkItem in Swift Hindi tutorial

Поділитися
Вставка
  • Опубліковано 2 сер 2024
  • DispatchWorkItem class in swift can be used when you want to cancel a task queued in grand central dispatch GCD. Mostly a question which is asked in interviews is if you can cancel a task in GCD and we say no and prefer to use Operation queues for that but you can actually allocate your task in DispatchWorkItem class and based on your logical or business condition cancel it whenever you see fit.
    In this video, we will be seeing a real project scenario where you can use DispatchWorkItem to cancel and task and to optimize an existing module. We will learn how we can use the debounce technique and improve our code with DispatchWorkItem and DispatchQueue.
    Given are the chapters covered in this video
    0:00 introduction
    0:14 prerequisite
    0:48 complain about gcd
    1:16 dispatchWorkItem class
    1:23 codecat15 example
    2:03 Pseudocode dispatchWorkItem
    2:24 dispatchWorkItem example
    4:45 dispatchWorkItem cancel example
    8:51 real project scenario for using dispatchWorkItem
    10:25 issue with the current implementation
    11:02 debounce explanation
    11:40 implementing debounce approach
    12:48 need for work item reference explained
    13:26 creating a work item reference
    13:59 code explanation
    14:36 debounce in action
    16:00 conclusion
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ✅ Subscribe to the channel and stay ahead of everyone by following the right way for writing clean code in swift. 🔥🔥🔥🔥🔥
    🔗Click on the link to subscribe: ua-cam.com/users/codecat?sub...
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Introduction to threading video link: • Threads in iOS Swift T...
    -----------------------------------------------------------------------------------------------------------------------
    📥 Download the source code from
    Note: The playground code shown in the video is also included
    Github: github.com/codecat15/UA-cam-...
    Google Drive: drive.google.com/file/d/1Z5go...
    -----------------------------------------------------------------------------------------------------------------------
    ############################################
    Connect with me on social media
    📷:: / codecat15
    📧: codecat15@gmail.com
    🐦: / codecat15
    Facebook: / code.cat.792
    Github: github.com/codecat15
    ###########################################
    #dispatchWorkItem #gcd #threadingInSwift

КОМЕНТАРІ • 60

  • @polu9132
    @polu9132 3 роки тому +9

    Sir, you are a real gem for the iOS developers. 🙏

    • @CodeCat15
      @CodeCat15  3 роки тому

      Thank you 😊 I am glad the video was helpful. Plz do ask questions on this topic and do share the video with your iOS group

    • @polu9132
      @polu9132 3 роки тому

      @@CodeCat15 Sir pls make more videos on concurrency on iOS like Operation Queues and some more advanced concepts if possible. Thanks.🙏

    • @CodeCat15
      @CodeCat15  3 роки тому +1

      Sure will do Polu

  • @GovindSingh-vf7ff
    @GovindSingh-vf7ff 2 роки тому +1

    Trust me this is my first comment in any ios development video , amazing explanation

    • @CodeCat15
      @CodeCat15  2 роки тому

      Welcome to the channel Goving and thank you for taking time and commenting on the video, I am glad the contents of this channel are helpful :)

  • @abhijnanmaji2682
    @abhijnanmaji2682 2 роки тому

    Great content sir. Grateful to access such content for free.

  • @akshaygajarlawar8878
    @akshaygajarlawar8878 3 роки тому +1

    Hey Ravi, I don't have words for how to thank you. Just ming blowing bhava!

    • @CodeCat15
      @CodeCat15  3 роки тому

      Akshay, welcome bhava, tula kahi doubts asel tar te pan vichar ani tuja iOS group sobat pan he channel share kar 👍

  • @dhavalnena7386
    @dhavalnena7386 2 роки тому +1

    Great topic with great example and explanation! Very helpful.

    • @CodeCat15
      @CodeCat15  2 роки тому

      Thanks Dhaval, glad the content is helpful. Do share the video with your iOS group and feel free to ask questions on the topic

  • @Mandhata9
    @Mandhata9 2 роки тому

    Thank you for this video.

  • @pratapapptunix3512
    @pratapapptunix3512 Рік тому

    Awesome Video
    Thanks for share brother

  • @icodingschool9018
    @icodingschool9018 3 роки тому +1

    yes bro I learned new concept DispatchWorkItem from this video . Thank you :-)

    • @CodeCat15
      @CodeCat15  3 роки тому

      Am glad this was helpful 👍

  • @darshantelang1535
    @darshantelang1535 3 роки тому +1

    Excellent explanation. These videos are fulfilling the gap for me of not being trained professionally as trainee and using random techniques, internet solutions to solve problems in task deadline and later sit and regret for writing bad code and waste further more time to clean the previous code. I am feeling a technical detox by watching several of these videos on daily basis. Kudos to the creator. #CheersToCodeCat15

    • @CodeCat15
      @CodeCat15  3 роки тому +2

      I like the term technical detox, funny thing is implementing clean code takes a little time but has long term benefits specially with regards to change requests, we surely don't want our codebase to be like a card house, you remove or replace one card and the entire house collapses.

  • @akashrevanna5327
    @akashrevanna5327 3 роки тому

    Way of explaining 🙏🙂

  • @Cartoon_FunStory
    @Cartoon_FunStory 3 роки тому

    Superb sir 👌👌

  • @NeerajKumar-tx1qo
    @NeerajKumar-tx1qo 3 роки тому +1

    Bhai ekdum mst sikhato ho, thank you🙏🙏🙏

    • @CodeCat15
      @CodeCat15  3 роки тому

      Thanks Neeraj, koi question raha ye topic pe to please puchna zaroor aur aapne iOS group mai bhi is video ko share karna :)

  • @aafaqahmed2265
    @aafaqahmed2265 2 роки тому

    Very well done

  • @indianbachelorkitchen6410
    @indianbachelorkitchen6410 2 роки тому +1

    Brilliancy ,bhai please do more and more videos ,,,,

    • @CodeCat15
      @CodeCat15  2 роки тому

      Threading playlist link: ua-cam.com/play/PLb5R4QC2DtFuSE3wpJAqvFRbal5-7sAwr.html
      Hope it helps

    • @indianbachelorkitchen6410
      @indianbachelorkitchen6410 2 роки тому

      @@CodeCat15 ya bhai making notes out of all videos from the playlist... Thanks bahi really, har baar interview ma a topic pareshaan karta tha ....now it will not...

  • @IleshPanchalOnly
    @IleshPanchalOnly 3 роки тому

    Awesome 😎

  • @hardipgajera2316
    @hardipgajera2316 3 роки тому +1

    clear all my doubt Thank you sir.

  • @ashishjaiswal3941
    @ashishjaiswal3941 3 роки тому

    Ravi, you are Super.

  • @sharmiv1060
    @sharmiv1060 2 роки тому +1

    Very useful content.. keep rocking

  • @MrVivekkumarpal
    @MrVivekkumarpal 2 роки тому

    👌👌👌

  • @fitdevelopertv5148
    @fitdevelopertv5148 3 роки тому +1

    informative

  • @PrashantKumar-fg4tg
    @PrashantKumar-fg4tg 3 роки тому

    Thanks for this information. Please create a video on operation queue also

    • @CodeCat15
      @CodeCat15  3 роки тому +1

      Hello Prashant, welcome to the channel I am glad this video was helpful. Please do feel free to ask questions on this topic and yes a video on operation queue and other threading concepts are on their way very soon

  • @unseenSky
    @unseenSky 3 роки тому +1

    I Think in iOS 13 there is a debounce api but thanks for the video, i learned how it works

    • @CodeCat15
      @CodeCat15  3 роки тому +1

      Indeed Somendra, using the combine framework we surely can implement debounce, but its from ios 13 onwards wish there was backward compatibility but its alright we have DispatchWorkItem to the rescue. Please feel free to ask questions on the topic and do share the channel with your iOS group

  • @vyankateshshivnikar7932
    @vyankateshshivnikar7932 Рік тому +2

    Very informative video Ravi....One doubt -> If we can cancel the task in GCD then what is the use of OperationQueue over it?

    • @CodeCat15
      @CodeCat15  Рік тому +1

      i made a video about this, please check it out and let me know if you have any queries
      video link: ua-cam.com/video/JuFAW7y8lp0/v-deo.html

    • @vyankateshshivnikar7932
      @vyankateshshivnikar7932 Рік тому

      @@CodeCat15 Thank you Ravi🙂

  • @ranganathc3170
    @ranganathc3170 3 роки тому +1

    nice vide0

  • @suryakantsharma6761
    @suryakantsharma6761 3 роки тому +1

    Bhai ek request hai apko code font is like android bro so change it to natural iOS. Thanks for content it was worthfull.

    • @CodeCat15
      @CodeCat15  3 роки тому

      Mai initially asp.net developer tha aur hum visual studio use karte the jiska default font hai consolas aur abhi 10 saalo se vahi font ki adat ho chuki hai koi bhi editor ho meri font family fixed hai, maine try kiya tha xcode ka default font but then jama nahi mujhe sorry agar font ka vajaha se koi dikkat aai aapko 🙏

    • @suryakantsharma6761
      @suryakantsharma6761 3 роки тому

      @@CodeCat15 Nahi bro I also had experience with Visual studio and remember this font haha 😛. no issues.

  • @vashusaini021
    @vashusaini021 4 місяці тому

    Brother
    I did not get this can someone please explain me.
    How DispatchWorkItem is cancelling the task? isCancellled bool which we checking here in for loop this bool variable is used for cancelling this for loop task (even cancelling by developer using break) not workitem.

  • @Sameerkhan-pu7kn
    @Sameerkhan-pu7kn 3 роки тому

    Same task can achieve by published cause publishe has time & cancel property.

    • @CodeCat15
      @CodeCat15  3 роки тому +1

      That’s more swiftui and am yet to explore that world of swiftui but it will be very interesting to see that and how it works or how it’s different from dispatchWorkItem I’ll make a note of this Sameer thanks for sharing ☺️

  • @vincentjoyhere
    @vincentjoyhere 3 роки тому +1

    One difference between dispatch work item and operation queue is -
    Dispatch work item can execute and cancel only one operation, whereas an operation queue can execute multiple operations concurrently and cancel together all if needed using the cancelAllOperations() method.
    Is that correct ?

    • @CodeCat15
      @CodeCat15  3 роки тому

      Apple recommends that you should always use the highest level of abstraction, which means we should be using operation queue and not GCD which is a low level api, but due to convince many devs including me go for gcd directly.
      Having said that, operation queue holds its place coz it can handle dependencies and you can pause and yes cancel all operations, operation queue does things more object oriented way than GCD and we have more control over what we are trying to do with operation queue than GCD

  • @ajaykumar-kq2wf
    @ajaykumar-kq2wf 2 роки тому +1

    Can we cancel api call in dispatchworkitem?

    • @CodeCat15
      @CodeCat15  2 роки тому

      You can cancel the work item itself which will ultimately cancel whatever workItem it was intent to do
      What this means is the API call which is already done to the server that call may happen and say before you receive the response if you cancel the workItem then in that case the response will be ignored.
      In theory I believe this is what should happen but you can try this out and confirm if this works as per your given usecase.

  • @keertikagupta
    @keertikagupta 3 роки тому +1

    Why are we calling the API on main queue?

    • @CodeCat15
      @CodeCat15  3 роки тому

      Hey Keertika, url session internally calls the api on a background thread on its own so we don’t really have to specify or use a dispatch queue. If you watch the serial api call video there you can see this behavior
      Video link: ua-cam.com/video/II7bJmC64IY/v-deo.html
      Let me know if that answers your question and if not plz feel free to ask more.

    • @keertikagupta
      @keertikagupta 3 роки тому +1

      @@CodeCat15 Thank you sooo much 😊

  • @desijugaad908
    @desijugaad908 3 роки тому +2

    hello, I really enjoy your video.. so informative.. and to the point. hats off to you.
    It would be really helpful if you create a video on the topic of the different combination of sync and async block using different dispatch queues(serial, concurrent, main &global). I really got confused of the flow in some of the questions like below,
    func testx(){
    defer {
    print("--testx--")
    }
    DispatchQueue.main.async {
    print("testx")
    }
    print("--testx")
    }
    func testy(){
    defer {
    print("--testy--")
    }
    DispatchQueue.main.async {
    print("testy")
    }
    print("--testy")
    }
    func test(){
    print("01")
    testx()
    print("02")
    testy()
    print("03")
    }
    test()
    this will print:
    01
    --testx
    --testx--
    02
    --testy
    --testy--
    03
    testx
    testy
    here the dispatch asyn block executed at last why that so?

    • @ankurgupta2502
      @ankurgupta2502 3 роки тому

      Yep, I also face the similar problem in understanding this topic. @CodeCat please answer

    • @CodeCat15
      @CodeCat15  3 роки тому +1

      Let me make a video on this as comments could get a little lengthy

    • @desijugaad908
      @desijugaad908 3 роки тому

      ​@@CodeCat15 sure, that'll be great.