<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Prasanth  on Medium]]></title>
        <description><![CDATA[Stories by Prasanth  on Medium]]></description>
        <link>https://medium.com/@prasanth08.ios?source=rss-a2472cda92da------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*eWlEIt8KxrWP7M_Dd3P5Zw.jpeg</url>
            <title>Stories by Prasanth  on Medium</title>
            <link>https://medium.com/@prasanth08.ios?source=rss-a2472cda92da------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 00:48:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@prasanth08.ios/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding async and await in Swift ]]></title>
            <link>https://medium.com/@prasanth08.ios/understanding-async-and-await-in-swift-a205deaa2979?source=rss-a2472cda92da------2</link>
            <guid isPermaLink="false">https://medium.com/p/a205deaa2979</guid>
            <category><![CDATA[async-await-in-swift]]></category>
            <category><![CDATA[swift-concurrency]]></category>
            <dc:creator><![CDATA[Prasanth ]]></dc:creator>
            <pubDate>Tue, 11 Mar 2025 06:48:50 GMT</pubDate>
            <atom:updated>2025-03-11T07:10:38.774Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z6CvywFZTYYkpYZb1Jmolw.jpeg" /></figure><p>Swift introduced async and await in Swift 5.5 to simplify asynchronous programming, making it more readable and safer. Instead of using completion handlers or Grand Central Dispatch (GCD), we can now write asynchronous code in a sequential style.</p><h3>🔹 What is async?</h3><ul><li>Marks a function as asynchronous, allowing it to run in the background without blocking the main thread.</li><li>Must be used with await when calling the function.</li></ul><h3>🔹 What is await?</h3><ul><li>Pauses execution until the async function returns a result.</li><li>It does not block the thread; execution resumes when the async task is complete.</li></ul><blockquote>🔹 Basic Example of async &amp; await</blockquote><pre>func fetchData() async -&gt; String { <br>    return &quot;Hello, Swift Async!&quot; <br>} <br><br>Task { <br>    let message = await fetchData() <br>    print(message) // ✅ Output: &quot;Hello, Swift Async!&quot; <br>}</pre><p>✅ async makes fetchData() run in the background.<br>✅ await waits for the result before proceeding.</p><blockquote>🔹 Calling Multiple async Functions in Parallel (async let)</blockquote><pre>func fetchUserProfile() async -&gt; String { <br>    try? await Task.sleep(nanoseconds: 2_000_000_000) <br>    return &quot;👤 User Profile Loaded&quot; <br>} <br><br>func fetchNotifications() async -&gt; String { <br>    try? await Task.sleep(nanoseconds: 1_000_000_000) <br>    return &quot;🔔 5 New Notifications&quot; <br>} <br><br>Task { <br>    async let profile = fetchUserProfile() <br>    async let notifications = fetchNotifications() <br><br>    let userProfile = await profile <br>    let userNotifications = await notifications <br><br>    print(userProfile)       // ✅ Output after 2 sec: &quot;👤 User Profile Loaded&quot; <br>    print(userNotifications) // ✅ Output after 1 sec: &quot;🔔 5 New Notifications&quot; <br>}</pre><p>✅ Both tasks run at the same time, reducing total execution time.</p><blockquote>🔹 Handling Errors with async &amp; try await</blockquote><pre>func fetchDataFromServer() async throws -&gt; String { <br>    let success = Bool.random() // Simulate success or failure <br>    if success { <br>        return &quot;✅ Data received!&quot; <br>    } else { <br>        throw URLError(.badServerResponse) <br>    } <br>} <br><br>Task { <br>    do { <br>        let result = try await fetchDataFromServer() <br>        print(result) <br>    } catch { <br>        print(&quot;❌ Error: \(error.localizedDescription)&quot;) <br>    } <br>}</pre><p>✅ Errors must be handled using try await inside a do-catch block.</p><h3>Why is async required?</h3><p>When you use async let, you are <strong>declaring a concurrent task</strong> that runs in the background. Unlike await, which pauses execution until a function returns a result, async let starts the task immediately and allows the program to continue executing other code before awaiting its result.</p><blockquote>Example without async let (Sequential Execution)</blockquote><pre>Task {<br>    let profile = await fetchUserProfile() <br>    let notifications = await fetchNotifications() <br><br>    print(profile)       // Waits for fetchUserProfile() first<br>    print(notifications) // Executes only after fetchUserProfile() completes<br>}</pre><p>In this case, fetchUserProfile() <strong>must finish before</strong> fetchNotifications() starts, making it sequential execution.</p><blockquote>Example with async let (Parallel Execution)</blockquote><pre>Task {<br>    async let profile = fetchUserProfile() <br>    async let notifications = fetchNotifications() <br><br>    let userProfile = await profile <br>    let userNotifications = await notifications <br><br>    print(userProfile)      <br>    print(userNotifications) <br>}</pre><p>✅ fetchUserProfile() and fetchNotifications() start <strong>at the same time</strong>.<br>✅ await ensures that we wait for both results before using them.</p><p>🚀 Swift’s async and await make concurrency more readable, efficient, and safe. Start using them in your Swift projects today! 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a205deaa2979" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Harnessing the Power of Combine in Swift with MVVM ]]></title>
            <link>https://medium.com/@prasanth08.ios/harnessing-the-power-of-combine-in-swift-with-mvvm-da352a56b4b3?source=rss-a2472cda92da------2</link>
            <guid isPermaLink="false">https://medium.com/p/da352a56b4b3</guid>
            <dc:creator><![CDATA[Prasanth ]]></dc:creator>
            <pubDate>Wed, 21 Aug 2024 05:59:17 GMT</pubDate>
            <atom:updated>2024-08-21T05:59:17.152Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve recently completed a project that leverages Apple’s Combine framework within the MVVM pattern to streamline data handling and enhance the user experience in a login feature.</p><p>🔍 <strong>What is Combine?</strong></p><p>The Combine framework in Swift is a powerful tool introduced by Apple in iOS 13 to handle asynchronous events by combining values over time. It provides a declarative Swift API for processing values over time and managing event-driven programming, offering a modern alternative to traditional patterns like delegation, callbacks, and notifications.</p><h3>Key Concepts of Combine:</h3><p><strong>Publishers:</strong></p><ul><li>Sources of data or events that emit values over time.</li><li>Examples: Just, Future, PassthroughSubject, CurrentValueSubject.</li><li>They can send values, a completion signal, or an error.</li></ul><p><strong>Subscribers:</strong></p><ul><li>Receive and handle values from publishers.</li><li>Examples: Sink, Assign.</li><li>They connect to a publisher to start getting values.</li></ul><p><strong>Operators:</strong></p><ul><li>Methods to transform, filter, or combine publisher outputs.</li><li>Examples: map, filter, combineLatest.</li><li>They help create complex data processing chains.</li></ul><p><strong>Subjects:</strong></p><ul><li>Special publishers can also emit their own values.</li><li>Examples: PassthroughSubject, CurrentValueSubject.</li></ul><p><strong>Cancellable:</strong></p><ul><li>When a publisher is subscribed to, it returns a Cancellable object.</li><li>This object can stop the subscription, halting further values.</li></ul><p><strong>Schedulers:</strong></p><ul><li>Control where and how events are processed (e.g., main thread or background).</li><li>Examples: DispatchQueue, RunLoop.</li></ul><h3>Combine Framework Example with MVVM for Login Feature</h3><p>GitHub Link: <a href="https://github.com/Prasanth8Dev/Combine-Framework-Example.git">https://github.com/Prasanth8Dev/Combine-Framework-Example.git</a></p><h4>Objective:</h4><p>This project demonstrates how to implement a login feature using the Combine framework within the MVVM architecture in a UIKit-based iOS app.</p><p>Architecture:</p><ul><li><strong>View:</strong> The ViewController manages the user interface, including text fields for user ID and password, and a login button. It also handles user interactions and binds to the ViewModel for real-time updates and responses.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ULseQoGLdPD90Lw0QmcK6g.png" /></figure><ul><li><strong>ViewModel:</strong> The LoginViewModel handles the login logic. It uses @Published properties like jsonData, user, errorMessage, and isLoading to update the view according to the network responses.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7HgjF1JUA2bd49KYKspz-Q.png" /></figure><ul><li><strong>Model:</strong> The UserLoginModel represents the structure of the user login data. This model is used to store and manage the user information after a successful login.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kAFZI7-Hh8gd7hWeRtyrtw.png" /></figure><blockquote>Key Features:</blockquote><p><strong>User Interaction:</strong></p><ul><li>When the login button is tapped, the ViewController validates the input and triggers the login process in the LoginViewModel by passing the user’s credentials.</li></ul><p><strong>ViewModel Binding:</strong></p><p>The ViewController subscribes to the LoginViewModel’s published properties. This allows it to:</p><ul><li>Store the UserLoginModel object (userLoginModel) containing the logged-in user’s data.</li><li>Display or handle user login data (user).</li><li>Print or show any error messages (errorMessage).</li><li>Show or hide a loading indicator based on the request’s state (isLoading).</li></ul><p><strong>Asynchronous Handling with Combine:</strong></p><ul><li>The ViewController manages asynchronous events using Combine, ensuring updates occur on the main thread via .receive(on: DispatchQueue.main).</li><li>Subscriptions are stored in a cancellables set for efficient memory management.</li></ul><p><strong>Model Integration:</strong></p><ul><li>The login data returned from the network request is stored in a UserLoginModel instance (userLoginModel), allowing the ViewController to manage and use this data throughout the app.</li></ul><p><strong>Error Handling and Loading States:</strong></p><ul><li>The LoginViewModel communicates errors and loading states, enabling the ViewController to provide appropriate feedback and manage the user experience effectively.</li></ul><p>This project effectively demonstrates how to use the Combine framework with MVVM, including the integration of a data model (UserLoginModel), creating a structured, responsive, and maintainable login feature.</p><p>If you have any questions or need clarification, don’t hesitate to ask. Keep coding with confidence! 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da352a56b4b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>