Close Menu
  • Home
  • AI
  • Big Data
  • Cloud Computing
  • iOS Development
  • IoT
  • IT/ Cybersecurity
  • Tech
    • Nanotechnology
    • Green Technology
    • Apple
    • Software Development
    • Software Engineering

Subscribe to Updates

Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

    What's Hot

    At his OpenAI trial, Musk relitigates an old friendship

    April 29, 2026

    ios – In Swift I’m trying to understand the flow of logic when using Sendable, nonisolated, and Actors

    April 29, 2026

    Hardware Hacking Goes Mobile – Hackster.io

    April 28, 2026
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Big Tee Tech Hub
    • Home
    • AI
    • Big Data
    • Cloud Computing
    • iOS Development
    • IoT
    • IT/ Cybersecurity
    • Tech
      • Nanotechnology
      • Green Technology
      • Apple
      • Software Development
      • Software Engineering
    Big Tee Tech Hub
    Home»iOS Development»ios – In Swift I’m trying to understand the flow of logic when using Sendable, nonisolated, and Actors
    iOS Development

    ios – In Swift I’m trying to understand the flow of logic when using Sendable, nonisolated, and Actors

    big tee tech hubBy big tee tech hubApril 29, 2026004 Mins Read
    Share Facebook Twitter Pinterest Copy Link LinkedIn Tumblr Email Telegram WhatsApp
    Follow Us
    Google News Flipboard
    ios – In Swift I’m trying to understand the flow of logic when using Sendable, nonisolated, and Actors
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    My understanding is the TripsFeedViewModel runs on the MainActor due to observable so this class is basically isolated to an actor, the MainActor. So in a sense it does prevent data races. Then when we tripService.getTrips() the main thread will pass it to a a background thread to wait in the queue/lock of the TripService actor. Once the thread is allowed in the TripService actor it initializes the NetworkRequestService which is then bound to that actor and so would it’s functions. So when we call sendRequest which is a method of the NetworkRequestService it would run on the TripService actor and the thread would wait until there is a response back, holding how the lock. However, is we mark is as nonisolated it can be passed to a background thread to complete then a thread can pick it up and enter the queue again. so that’s why we have to mark it as nonisolated and the NetworkRequestService as sendable because its safe to send across threads since there are no mutable states. Then when it comes to the PrivateTripResponse it is technically always sendable because its a struct and a copy is created instead of having references to it. Since PrivateTripResponse conforms to Codable it runs on the MainActor so marking it as nonisolated lets it get decoded on a background actor as well and its safe because its sendable. So sendbale and nonisolated is working hand and hand letting the complier know is something is thread-safe to send across threads with data races. Would this explanation capture what is going on in this entire flow?

    import Foundation
    
    @Observable
    class TripsFeedViewModel {
        var trips: [Trip] = []
        
        private let tripService: TripServiceProtocol
        
        init(tripService: TripServiceProtocol) {
            self.tripService = tripService
        }
        
        func getTrip() async -> Void {
            do {
                let trips = try await tripService.getTrips()
                await MainActor.run {
                    self.trips = trips.compactMap {
                        Trip(
                            id: $0.id,
                            tripName: $0.tripName,
                            location: $0.location,
                            budget: $0.budget,
                            isFavorite: $0.isFavorite,
                            startDate: $0.startDate,
                            endDate: $0.endDate,
                            imageURLString: $0.imageURL
                        )
                    }
                }
            } catch {
                print("There was an error get your trips: \(error.localizedDescription)")
            }
        }
    }
    
    import Foundation
    
    actor TripService: TripServiceProtocol {
        private let networkService: NetworkRequestService
        private let keychainService: KeychainService
        private var activeTask: Task<[TripPrivateResponse], Error>?
    
        init(networkService: NetworkRequestService, keychainService: KeychainService) {
            self.networkService = networkService
            self.keychainService = keychainService
        }
        
        func getTrips() async throws -> [TripPrivateResponse] {
            if let existing = activeTask {
                return try await existing.value
            }
            
            let task = Task<[TripPrivateResponse], Error> {
                guard let url = URL(string: "local host url") else {
                    throw APIError.invalidURL
                }
                
                var request = URLRequest(url: url)
                request.httpMethod = "GET"
                
                if let token = keychainService.getToken() {
                    request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
                }
                
                return try await networkService.sendRequest(request: request, responseType: [TripPrivateResponse].self)
            }
            
            activeTask = task
            
            defer { activeTask = nil }
            
            return try await task.value
        }
    }
    
    struct TripPrivateResponse: nonisolated Codable, Sendable {
        let id: Int
        let tripName: String
        let location: String
        let budget: Int
        let isFavorite: Bool
        let startDateString: String
        let endDateString: String
        let imageURLString: String
        
        enum CodingKeys: String, CodingKey {
                case id
                case tripName = "title"
                case location
                case budget
                case isFavorite = "is_favorite"
                case startDateString = "start_date"
                case endDateString = "end_date"
                case imageURLString = "cover_image_url"
            }
    }
    
    final class NetworkRequestService: Sendable {
        // MARK: Sends the request and returns the response from FastAPI
        nonisolated func sendRequest(request: URLRequest, responseType: Output.Type) async throws -> Output {
            do {
                /// 1. this sends the data to FastAPI then waits for a response
                let (data, response) = try await URLSession.shared.data(for: request)
                
                print("FASTAPI RESPONSE: \(String(data: data, encoding: .utf8) ?? "No Data")")
                
                /// 2. checks the response (convert it to HTTPURLResponse type) making sure it has a successful status code
                guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
                    throw APIError.invalidResponse
                }
                
                /// 3. decode it to the DTO (UserPrivateResponse)
                return try JSONDecoder().decode(responseType, from: data)
            } catch let error as URLError {
                throw APIError.networkError(error)
            } catch let error as DecodingError {
                throw APIError.decoding(error)
            }
        }
    }
    



    Source link

    actors flow iOS Logic nonisolated Sendable Swift understand
    Follow on Google News Follow on Flipboard
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Copy Link
    tonirufai
    big tee tech hub
    • Website

    Related Posts

    Introducing SwiftBash | Cocoanetics

    April 28, 2026

    Delphi + PAServer: How to correctly export iOS app with Notification Service Extension (appex) – missing provisioningProfiles in exportOptions.plist

    April 27, 2026

    ios – Apply shadow only outside of a SwifUI transparent button?

    April 26, 2026
    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    At his OpenAI trial, Musk relitigates an old friendship

    April 29, 2026

    ios – In Swift I’m trying to understand the flow of logic when using Sendable, nonisolated, and Actors

    April 29, 2026

    Hardware Hacking Goes Mobile – Hackster.io

    April 28, 2026

    AWS Weekly Roundup: Anthropic & Meta partnership, AWS Lambda S3 Files, Amazon Bedrock AgentCore CLI, and more (April 27, 2026)

    April 28, 2026
    Timer Code
    15 Second Timer for Articles
    20
    About Us
    About Us

    Welcome To big tee tech hub. Big tee tech hub is a Professional seo tools Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of seo tools, with a focus on dependability and tools. We’re working to turn our passion for seo tools into a booming online website. We hope you enjoy our seo tools as much as we enjoy offering them to you.

    Don't Miss!

    At his OpenAI trial, Musk relitigates an old friendship

    April 29, 2026

    ios – In Swift I’m trying to understand the flow of logic when using Sendable, nonisolated, and Actors

    April 29, 2026

    Subscribe to Updates

    Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

      • About Us
      • Contact Us
      • Disclaimer
      • Privacy Policy
      • Terms and Conditions
      © 2026 bigteetechhub.All Right Reserved

      Type above and press Enter to search. Press Esc to cancel.