How to use Task and .task in Swift | Swift Concurrency #4

Поделиться
HTML-код
  • Опубликовано: 9 ноя 2024

Комментарии • 68

  • @robertsandru8131
    @robertsandru8131 Год назад +7

    The fact that you came back to this video to add the checkCancellation part made me proud of being your subscriber. After years of iOS development, your content & teaching skills feel like something new to me. Thank you!

  • @vladyslavsushko1967
    @vladyslavsushko1967 8 месяцев назад +2

    Thank you, Nick. I am very happy that i can watch this all brilliant structured content in 2024, and can't even imagine how much the subscribers suffered a few years ago when they were waiting for new videos. Best courses and absolutely free 👒

  • @RobertHolzapfel
    @RobertHolzapfel 2 года назад +16

    Dear Nick, THANK YOU SO MUCH for your outstanding tutorials. I love, that you always start with a plain, virgin new project (no downloads necessary), I love, that you systematically unfold the topics, though straight forward and most importantly: It is advanced stuff, not just for beginners. Really, really fantastic and helpful tutorials. Thanks and sorry, in case my English is a bit "German".

  • @muhammadnasser2735
    @muhammadnasser2735 2 года назад

    I think your channel will be the most powerful educational channel in the world 🗺

  • @kenjyfukuda
    @kenjyfukuda 2 года назад +1

    Thank you for your exhaustive and comprehensive explanation.

  • @theflyingsolo
    @theflyingsolo 2 года назад +1

    Gotta love that this is “not rocket science”. 🙌🏼

  • @josemanuelortizsanchez4983
    @josemanuelortizsanchez4983 2 года назад +1

    Thanks, Nick!

  • @mrtayyabmughal7209
    @mrtayyabmughal7209 2 года назад +2

    Awesome Task!

  • @CalmVink
    @CalmVink Год назад +2

    One more thing about task cancellation: if your function does not throw an error, you can use the Task.isCancelled property. For example, in a for loop, you can break it like this: if Task.isCancelled { break }.

    • @dre5671
      @dre5671 11 месяцев назад +1

      Great tip! Thanks for sharing Zoli!

  • @pavanmanjani7583
    @pavanmanjani7583 2 года назад +1

    Superb, Thanks

  • @ekadam
    @ekadam 2 года назад +1

    super cool videos! To the point.

  • @andresraigoza2082
    @andresraigoza2082 2 года назад +1

    Thank you so much Nick!!!

  • @r_celikkaya
    @r_celikkaya 2 года назад

    your expressions very useful and very instructive.

  • @antonignatenko7776
    @antonignatenko7776 6 месяцев назад +1

    interesting i am absolutely new to swift, just learning the basics but have experience in java. In java it's considered an absolutel no no to use optionals as properties, but i see it's fine in swift

    • @SwiftfulThinking
      @SwiftfulThinking  6 месяцев назад +1

      We love optionals, as long as they are safely unwrapped!

  • @hashcat5721
    @hashcat5721 2 года назад +2

    12:34 All this should be done in xCode 14 and you will see that the calling order is correct there, unlike before. Tasks are called exactly as the priority is specified !

  • @emdutt4012
    @emdutt4012 Год назад

    again amazing video nick

  • @bruhmoment3731
    @bruhmoment3731 2 года назад

    Another excellent tutorial! Thank you!

  • @peterbien4049
    @peterbien4049 2 года назад

    Nick, first of all thank you for the very well done tutorial. I find the option to simply use .task instead of OnAppear strong and very secure. Only if the 'await' fetch function used in .task has to be called again, e.g. when using .sheet(item: $modal, onDismiss: { .. Task {} } ...
    would you then explicitly cancel all tasks in the OnDismiss { }?
    I hope I have expressed myself in a way that you understand my question😅

  • @helloworld4303
    @helloworld4303 4 месяца назад

    Thank you for the video! Out of curiosity, do people still get the purple error around 24:12? As of Swift 5.10 (June 2024 now) I followed everything he did up to this point and did not have to do an "await MainActor"

  • @98motya
    @98motya 2 года назад

    Nick! Will you teach us how to use combine with async/await to asynchronously update our views?

  • @OhreoOhs
    @OhreoOhs 2 года назад

    Thank yo for This super lectures. amazing!

  • @hashcat5721
    @hashcat5721 Год назад

    The priority of the tasks at application startup changes if we do a .push to these tasks from another application screen after startup. They will run in priority from high to low.

  • @Jock3R87
    @Jock3R87 2 года назад +5

    Mate you no need to write Mainactor always... Just mention @MainActor before your class and wherever UI update line is there it will take care automatically

  • @RajeshSharmaIndia
    @RajeshSharmaIndia Год назад

    Hi , Thanks for your video, i have a query , what is difference between async function and Task in swift.

  • @priyapriya7062
    @priyapriya7062 6 месяцев назад

    Man your Awesome!!!

  • @alexeylobankov
    @alexeylobankov 2 года назад +2

    What if I have 2 async method that I want to start from my view onAppear and run them in parallel. Should I then use 2 separate .task view modifiers?

    • @SwiftfulThinking
      @SwiftfulThinking  2 года назад +4

      You can, or you can use async let.. which is the next video in this series 🤙

  • @dre5671
    @dre5671 11 месяцев назад

    I may have missed it, but how the heck did we get "cancelled" in the console? 27:54
    Anyway, great video! Learned a lot!

    • @lolrie
      @lolrie 11 месяцев назад

      It comes automatically when we add the code for .onDisappear at the bottom of the view:
      .onDisappear {
      fetchImageTask?.cancel()
      }

  • @MarkusSwiftUI
    @MarkusSwiftUI Год назад +4

    Task.sleep(for: .seconds(5))

  • @danstoian7721
    @danstoian7721 2 года назад +1

    9:53 In SwiftUI you cannot publish changed of your observable object, aka view model, from any other thread that UI - main thread. How do you make sure your Tasks run in the main thread?

    • @andrewli1455
      @andrewli1455 2 года назад +2

      MainActor.run {
      } instead of dispatch queue

  • @moon-3002
    @moon-3002 2 года назад +1

    In previous video Task.sleep makes the rest of the code move to background thread. But why in this video it shows on main thread 16:06 ? Can someone help?

    • @Mrbr83
      @Mrbr83 Год назад +1

      Good question! I would say with async/await in general there is no deterministic behavior in the sense of what thread the code is executed on. It's out of the caller's control.

  • @muhammadnasser2735
    @muhammadnasser2735 2 года назад

    man thank you so mach 🙂 you are awesome

  • @RazvanLitianu
    @RazvanLitianu 2 года назад +1

    Hi, you can write `2 * NSEC_PER_SEC` so you don't have to use billions. Hope it helps!

  • @bobbynwm
    @bobbynwm Год назад

    Excellent videos as usual! Nick mate, one quick question: I am watching this tutorial 1 yr after you published it and get this alert in the code when calling Thread.current in the print out, and no idea to fix it: "Class property 'current' is unavailable for asynchronous contexts; Thread.current cannot be used from async context; this is an error in Swift 6" - does this mean Apple is going to fix it ? Any thoughts ?

    • @SwiftfulThinking
      @SwiftfulThinking  Год назад +3

      I’m just using Thread.current for the video purposes. It’s not part of the actual code you would put in your app. I would just ignore it for now.

    • @bobbynwm
      @bobbynwm Год назад

      @@SwiftfulThinking cool, thanks

    • @spradohak
      @spradohak Год назад +1

      you can use just Thread() and it will give you the actual thread as Thread.current.

  • @ritzg98
    @ritzg98 8 месяцев назад

    At around 9:00, there's a reference to Thread.current. In my version of XCode, that returns a warning. Thread.current is apparently not allowed in Swift 6. Does anyone know an alternative to this?

    • @SwiftfulThinking
      @SwiftfulThinking  8 месяцев назад

      We’re just using it for some print messages, it’s not actually part of the production code

  • @andrewdunbar828
    @andrewdunbar828 Год назад

    I could not find any way to do something after Task {} finishes. So far I've been able to rearrange things so that Task {} is further up the call stack so that all the async is inside it. But I can imagine that sooner or later that won't be enough and then I'm not sure what to do.

  • @y0utak
    @y0utak 2 года назад

    .task is super Strong

  • @tbg6nb
    @tbg6nb 2 года назад +1

    22:30 one can write UInt64(5e9)

    • @rwilson1125
      @rwilson1125 Год назад

      @Swiftful Thinking or user Task.sleep( for: .seconds( 2 .1 ) )

  • @vladimirstepanov7246
    @vladimirstepanov7246 2 года назад

    Super)

  • @srinivasprayagsahu1375
    @srinivasprayagsahu1375 Год назад

    F*ck dude, i really found it amazing. Awesome.

  • @WJCNWJCN
    @WJCNWJCN 6 месяцев назад

    Task.sleep(for:tolerance:clock:) in iOS 16 is nicer than Task.sleep(nanoseconds:) in iOS 13.

  • @merakshot9277
    @merakshot9277 6 месяцев назад

    Hey Nick but how do i use .task for example in Button(action: _ )

    • @merakshot9277
      @merakshot9277 6 месяцев назад

      Trailing closure passed to parameter of type 'BackingData' that does not accept a closure
      when i tried Tas { await someFunc() } it starts giving this error in Xcode 15, but before i migrated from Xcode 14 it was working, and i followed up whole course concurrency, but now it doesn't work for me i don't really know why. please help me.

    • @SwiftfulThinking
      @SwiftfulThinking  6 месяцев назад

      @@merakshot9277within Button you need to use Task {

    • @merakshot9277
      @merakshot9277 6 месяцев назад

      @@SwiftfulThinking
      BookmarkButton(
      viewModel.errorMessage ?? "Save To Collection",
      progress: $viewModel.progress,
      showLoader: viewModel.showLoader,
      bookmarkFailed: viewModel.errorMessage != nil
      ) {
      async {
      await didTapBookmark()
      }
      }
      i had already Task { await didTapBookmark() }
      but i saw that it keeps throwing ( Trailing closure passed to parameter of type 'BackingData' that does not accept a closure)
      and i used async { } but that one is deprecated and my code works.

  • @franciscoarciganolasco6027
    @franciscoarciganolasco6027 9 месяцев назад

    i have a doubt, the Task is a Thread-Like?

    • @SwiftfulThinking
      @SwiftfulThinking  9 месяцев назад

      Similar but not the same. Every time you have “await” there is potential for the Task so switch threads

  • @andrewdunbar828
    @andrewdunbar828 Год назад

    What are you able to use .task with? I can't find anything that has a .task member.
    Oh it's just in SwiftUI? My current project is in AppKit.

  • @Ehsan_15
    @Ehsan_15 2 года назад

    Could you please share code of the series?

  • @faikcoskuncakr7418
    @faikcoskuncakr7418 2 года назад +1

    Thank you so much

  • @SanjeebGhimire-w6j
    @SanjeebGhimire-w6j Год назад

    can Anyone help me??
    There comes an error "Extra trialing closure passed in a call" if i use Task{} inside .onAppear{}. How to solve this?

  • @peterbien2879
    @peterbien2879 2 года назад

    If I follow your example, and use fetchImageTask?.cancel() in .OnDisappear() I get the error: The operation couldn’t be completed. (Swift.CancellationError error 1.). What am I doing wrong?

  • @alessandrodimaio589
    @alessandrodimaio589 2 года назад

    I usually use (also if apple say don't use it) after a lot of investigation Task.detached { @MainActor in } in this way the task inside and eventually task groups are executed in another thread and there are no UI dispatch problems. Otherwise everything will be executed on the main Thread.

    • @alessandrodimaio589
      @alessandrodimaio589 2 года назад

      Also the .task {} swiftUI modifier execute everything on main thread