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!
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 👒
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".
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 }.
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
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 !
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😅
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"
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.
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
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?
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?
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.
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 ?
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?
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.
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 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.
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?
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.
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!
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 👒
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".
I think your channel will be the most powerful educational channel in the world 🗺
Thank you for your exhaustive and comprehensive explanation.
Gotta love that this is “not rocket science”. 🙌🏼
Thanks, Nick!
Awesome Task!
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 }.
Great tip! Thanks for sharing Zoli!
Superb, Thanks
super cool videos! To the point.
Thank you so much Nick!!!
your expressions very useful and very instructive.
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
We love optionals, as long as they are safely unwrapped!
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 !
again amazing video nick
Another excellent tutorial! Thank you!
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😅
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"
Nick! Will you teach us how to use combine with async/await to asynchronously update our views?
Thank yo for This super lectures. amazing!
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.
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
Hi , Thanks for your video, i have a query , what is difference between async function and Task in swift.
Man your Awesome!!!
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?
You can, or you can use async let.. which is the next video in this series 🤙
I may have missed it, but how the heck did we get "cancelled" in the console? 27:54
Anyway, great video! Learned a lot!
It comes automatically when we add the code for .onDisappear at the bottom of the view:
.onDisappear {
fetchImageTask?.cancel()
}
Task.sleep(for: .seconds(5))
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?
MainActor.run {
} instead of dispatch queue
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?
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.
man thank you so mach 🙂 you are awesome
Hi, you can write `2 * NSEC_PER_SEC` so you don't have to use billions. Hope it helps!
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 ?
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.
@@SwiftfulThinking cool, thanks
you can use just Thread() and it will give you the actual thread as Thread.current.
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?
We’re just using it for some print messages, it’s not actually part of the production code
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.
.task is super Strong
22:30 one can write UInt64(5e9)
@Swiftful Thinking or user Task.sleep( for: .seconds( 2 .1 ) )
Super)
F*ck dude, i really found it amazing. Awesome.
Task.sleep(for:tolerance:clock:) in iOS 16 is nicer than Task.sleep(nanoseconds:) in iOS 13.
Hey Nick but how do i use .task for example in Button(action: _ )
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.
@@merakshot9277within Button you need to use Task {
@@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.
i have a doubt, the Task is a Thread-Like?
Similar but not the same. Every time you have “await” there is potential for the Task so switch threads
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.
Could you please share code of the series?
Thank you so much
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?
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?
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.
Also the .task {} swiftUI modifier execute everything on main thread