Malcolm Hall

  • About
  • Research
  • Consultancy
  • Products
    • AppRecorder
    • Apps Using Location
    • Clipboard Monitor
    • Common Rocks Reference
    • Core Location Info
    • Friendify
    • Grape Stomper
    • HomeMenu
    • iBeacon Scanner
    • Insomnia
    • Navigate from Maps
    • Nickname
    • Plate Smasher
    • Plugged In
    • Readster
    • Resize Image
    • Read★
    • ScreenshotMenu
    • Sleep Display
    • SleepFX
    • Stealth Cam
    • TV Tube Sleep
    • UDID Calculator
    • WiFi Booster
    • WiFi Passwords

Concurrency

Concurrency

Converting from @StateObject to .task

To upgrade old Combine code in an ObservableObject that had its lifetime tied to a SwiftUI View using @StateObject we can replace it with .task and since async/await gives us a lifetime like a reference type we can do away with the object altogether. In the Combine version of the Read more…

By malhal, 4 months19th February 2025 ago
Concurrency

CLMonitor singleton to workaround the “Monitor named is already in use” exception

If you attempt to init another instance of CLMonitor with the same name it crashes with: Since I wanted to use the same monitor from different tasks I needed to turn it into a singleton and came up with this: This allows this kind of thing to work without crashing: Read more…

By malhal, 1 year15th May 2024 ago
Concurrency

A simple calculator using @Observable, withObservationTracking and AsyncStream

Following on from my previous post on this topic I found a forum post with someone struggling to build a calculator using @Observable and thought maybe my technique of wrapping withObservationTracking in AsyncStream might help and came up with the code below which appears to work. Obviously this code comes Read more…

By malhal, 1 year14th May 2024 ago
Concurrency

Create an AsyncStream from withObservationTracking() function

I needed to create an AsyncStream to monitor changes to an @Observable model class’s var and by using withCheckedContinuation and AsyncStream‘s unfolding init I came up with this: The challenge here is withObservationTracking only watches for a single change but we can wrap that in withCheckedContinuation which also is designed Read more…

By malhal, 1 year4th May 2024 ago
Concurrency

Merge different AsyncStream types

When I first used AsyncStream with merge from AsyncAlgorithms I struggled with merging different stream result types and had experimented with building a protocol that each stream’s result type enum implemented. That way all the streams used the same protocol type and could be merged. I realised today that I Read more…

By malhal, 1 year18th March 2024 ago
Concurrency

Capturing ‘async let’ variables is not supported

Discovered this strange error in Swift, when using the sample code from the proposal which is supposed to have been accepted and implemented

By malhal, 2 years7th December 2023 ago
Concurrency

Async/await UIAlertController

I needed to show an alert in an async context and this is how I achieved it: The idea to throw a cancellation exception was inspired by DDDevicePickerViewController which is the only built-in async UIViewController I’m aware of.

By malhal, 2 years15th October 2023 ago
Concurrency

Async/await command line tool in Xcode 14.3

Update 10/4/2024: It appears in Xcode 15.3 nothing special is required and can use await right next to the hello world print. TLDR: add @main and rename main.swift To create a command line tool in Xcode 14.3 that uses async/await there are a few tweaks required. When we create the Read more…

By malhal, 2 years25th April 2023 ago
Concurrency

@MainActor does not always mean main thread

Seems to me some developers think that @MainActor means “always main thread” and has the same behaviour as DispatchQueue.main.async which I don’t think is try. For example, the code below outputs: Because the Database class is not marked as @MainActor then loadModel will in fact be on a background thread, Read more…

By malhal, 2 years31st January 2023 ago
Recent Posts
  • Converting from @StateObject to .task
  • @Observable and didSet
  • Dynamically filtering a SwiftData query
  • Interesting glitch with Measurement in SwiftUI
  • How to use withObservationTracking to monitor an @Observable
Recent Comments
  • SwiftUI 2: the way to open view in new window on Open window / scene in SwiftUI 2.0 on macOS
  • Why Does URL Scheme/onOpenURL In SwiftUI Always Open A New Window? - Programming Questions And Solutions Blog on Open window / scene in SwiftUI 2.0 on macOS
  • Ken Carroll on Enabling iOS 9.3 SDK in Xcode 8 for iOS 10
  • Mr WordPress on Welcome to my Developer Blog
Archives
  • February 2025
  • November 2024
  • October 2024
  • July 2024
  • May 2024
  • April 2024
  • March 2024
  • January 2024
  • December 2023
  • October 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • January 2023
  • November 2022
  • June 2022
  • December 2020
  • May 2018
  • October 2017
  • September 2017
  • August 2017
  • January 2017
  • July 2016
  • May 2016
Categories
  • AppIntents
  • CloudKit
  • Concurrency
  • CoreData
  • CoreLocation
  • Mac
  • MapKit
  • NSOperation
  • Objective-C
  • Swift
  • SwiftData
  • SwiftUI
  • UIKit
  • Uncategorised
  • Xcode
Meta
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
  • About
  • AppRecorder
  • Apps Using Location
  • Clipboard Monitor
  • Common Rocks Reference
  • Consultancy
  • Core Location Info
  • Friendify
  • Grape Stomper
  • HomeMenu
  • iBeacon Scanner
  • Insomnia and Insomnia Pro for iPhone
  • Navigate from Maps
  • Nickname
  • Plate Smasher
  • Plugged In
  • Products
  • Projects
  • Read★
  • Readster
  • Research
  • Resize Image
  • Sample Page
  • ScreenshotMenu
  • Sleep Display
  • SleepFX
  • Stealth Cam
  • TV Tube Sleep
  • UDID Calculator
  • WiFi Booster
  • WiFi Passwords
Hestia | Developed by ThemeIsle