Learn how to effectively use the result of a `completionHandler` to set the destination of a NavigationLink in SwiftUI with step-by-step instructions.
---
This video is based on the question https://stackoverflow.com/q/72153099/ asked by the user 'JamMan9' ( https://stackoverflow.com/u/6394307/ ) and on the answer https://stackoverflow.com/a/72153409/ provided by the user 'HunterLion' ( https://stackoverflow.com/u/18251327/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How to use the result of a completionHandler for the destination of a NavigationLink in Swift?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
How to Use completionHandler Results for a Navigation Link Destination in SwiftUI
Navigating between views is a common task in SwiftUI applications. However, when working with asynchronous operations, like making API calls, it can become a bit complicated. A frequent challenge developers face is how to pass the retrieved data from an asynchronous function, such as one that uses a completionHandler, into the destination view of a NavigationLink. In this guide, we'll explore a solution for this problem, primarily for iOS apps, utilizing Swift’s async/await syntax.
Understanding the Problem
The goal is to execute an asynchronous function (in this case, an API call) when a user taps a NavigationLink, and subsequently navigate to a new view using the data received from that function. The fundamental issue lies in how to manage the asynchronous nature of API calls while still ensuring a smooth user experience.
Example Code Breakdown:
You have a function, postProgramme, that calls a REST API and uses a completionHandler to return the result:
[[See Video to Reveal this Text or Code Snippet]]
And the NavigationLink setup that you are using appears as follows:
[[See Video to Reveal this Text or Code Snippet]]
The challenge is how to link the results of postProgramme to the destination view.
Solution: Utilizing Async/Await
Starting with iOS 15, Swift introduced the async/await capabilities which simplify working with asynchronous operations. Below, we detail the steps to refactor your code.
Step 1: Refactor Your API Function
First, we convert your postProgramme function to use async/await. This allows the function to return a Programme directly instead of using a completionHandler.
[[See Video to Reveal this Text or Code Snippet]]
Step 2: Update Your View to Handle State
Define a @ State variable in your parent view for the Programme object that you will pass to the ProgrammeDetailView.
[[See Video to Reveal this Text or Code Snippet]]
Step 3: Use Task to Call Async Function
Within the createNewProgramme function, you will use a Task to call your async function. This allows you to await the result and update the state variable once the call completes.
[[See Video to Reveal this Text or Code Snippet]]
Step 4: Pass Data to the Destination View
When setting up the NavigationLink, pass the programme state variable as a binding to ProgrammeDetailView.
[[See Video to Reveal this Text or Code Snippet]]
Step 5: Handle Binding in the Destination View
In ProgrammeDetailView, you'll define a @ Binding variable to receive the Programme object. You'll also need to consider how to present the view while waiting for the data to load.
[[See Video to Reveal this Text or Code Snippet]]
Conclusion
Implementing asynchronous operations in SwiftUI can initially seem daunting, especially when it involves navigation. However, with the introduction of async/await, the process has been significantly streamlined. By following the steps outlined in this post, you'll be able to smoothly pass data from a completionHandler to a NavigationLink destination, leading to a better user experience in your SwiftUI applications. Happy coding!
Информация по комментариям в разработке