MainThreadPropertyAccessor

0.1.1

Syntactic sugar for setting properties on an ObservableObject on the main thread from within a Task.
theisegeberg/MainThreadPropertyAccessor

What's New

v0.1.1

2022-12-10T21:09:00Z

Before nil setting was made impossible to make the syntax slightly more readable. Instead the getter now fatal errors, which I consider to be more in line with the overall purpose. As in nil-setting is nice, and the getter shouldn't be used at all :-)

MainThreadPropertyAccessor

Syntactic sugar for setting properties on an ObservableObject on the main thread from within a Task.

Task {
    self.setOnMain.status = "Now I'll never get purple warnings again!"
}

Installation

You can use the Swift Package Manager by declaring MainThreadPropertyAccessor as a dependency in your Package.swift file:

.package(url: "https://github.com/theisegeberg/MainThreadPropertyAccessor", from: "0.1.0")

For more information, see the Swift Package Manager documentation.

The problem

ObservableObject is often used as a kind of controller for SwiftUI View. After structured concurrency (async/await) was introduced we'll often need to set the value of a @Published property inside of a ObservableObject. This means wrapping the code in DispatchQueue.main.async { ... } or using a @MainActor annotated method. This can be done, but it's a lot of code.

The solution

A protocol with no requirements that provides a default extension that exposes only one computed property: .setOnMain. This returns an object that can be subscripted into based on the KeyPath's of the ObservableObject that implements the protocol.

Usage

  1. Add the protocol MainThreadPropertyAccessor to your ObservableObject
  2. Use self.setOnMain.somePropertyOfYourObject = newValue to set properties on your ObservableObject
class AnObservableObject: ObservableObject, MainThreadPropertyAccessor {
    @Published var status:String = ""
    func updateStatus() {
        Task {
            // Do some async work
            self.status = "Will update the UI from a background thread" // Warning!
            self.setOnMain.status = "Will update the UI from the main dispatch queue" // 😘
        }
    }
}

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Oct 21 2024 22:57:10 GMT-0900 (Hawaii-Aleutian Daylight Time)