Challenge: Reinforce what you learned in Part 1 by implementing the list view in the SuperStorage app. Try not to peek at LittleJohn’s SymbolListView.
Thanks for another great course as usual!
Quick question around 4:17, where we used:
self.files = ...
from the task modifier. Since this line is run asynchronously from the task, shouldn’t the runtime show a warning that accessing files property from async task is not allowed and that it should only be mutated on the main thread (a.k.a MainActor.run {self.files = ...})
hi Srinath, interesting question! This article points out that the closure of a .task view modifier runs on the main thread. Add this line just before self.files = ...:
print(Thread.current)
and ignore the warning about this being an error in Swift 6. When you run the app, you’ll see:
<_NSMainThread: 0x6000032040c0>{number = 1, name = main}
Note that try await (files, status) runs availableFiles() and status()off the main thread — await always gives the system an opportunity to dispatch to another thread. Then they return to the main thread to update files and status.
Also, option click .task to see that Apple’s own sample code updates a State variable inside the .task closure.
The rules are different (simpler?) for SwiftUI views.