Posts

Post not yet marked as solved
2 Replies
323 Views
I followed that Apple tutorial about persisting data using async/await and tasks (https://developer.apple.com/tutorials/app-dev-training/persisting-data), I have to say that everything works fine when following the tutorial, but I thought a bit about that code and wondered why is there a Task created in the (throwing async) load method ? func load() async throws { let task = Task<[DailyScrum], Error> { let fileURL = try Self.fileURL() guard let data = try? Data(contentsOf: fileURL) else { return [] } let dailyScrums = try JSONDecoder().decode([DailyScrum].self, from: data) return dailyScrums } let scrums = try await task.value self.scrums = scrums } Wouldn't it be better to just do the following ? func load() async throws { let fileURL = try Self.fileURL() guard let data = try? Data(contentsOf: fileURL) else { return [] } self.scrums = try JSONDecoder().decode([DailyScrum].self, from: data) } From my understanding, doing the later allow to remove one task schedule on the main thread. I don't really see the benefit to do it like it's shown in the tutorial. If you have an argument that goes for the tutorial way, please let me know. If there's none found, why would Apple show some code that's more complex than necessary, and not best practice in its tutorials ?
Posted
by iamthedev.
Last updated
.