From 5d174b9cafb5c62449fb755973accf5498577527 Mon Sep 17 00:00:00 2001 From: Zach Date: Thu, 24 Oct 2024 19:53:30 -0600 Subject: [PATCH] Use Task MainActor to queue up set call (#118) * Use Task MainActor to queue up set call * Revert back old code, but keep Task { @MainActor --- .../Types/State/Application+State.swift | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/Sources/AppState/Application/Types/State/Application+State.swift b/Sources/AppState/Application/Types/State/Application+State.swift index 68ee441..eab6779 100644 --- a/Sources/AppState/Application/Types/State/Application+State.swift +++ b/Sources/AppState/Application/Types/State/Application+State.swift @@ -34,14 +34,27 @@ extension Application { ) else { defer { - shared.cache.set( - value: Application.State( - type: .state, - initial: _value, - scope: scope - ), - forKey: scope.key - ) + let setValue = { + shared.cache.set( + value: Application.State( + type: .state, + initial: _value, + scope: scope + ), + forKey: scope.key + ) + } + #if (!os(Linux) && !os(Windows)) + if NSClassFromString("XCTest") == nil { + Task { @MainActor in + setValue() + } + } else { + setValue() + } + #else + setValue() + #endif } return _value }