AppState

3.1.0

๐Ÿง  Effortless State Management and Dependency Injection for Swift 6 Applications
0xLeif/AppState

What's New

3.1.0

2026-11-16T23:25:07Z

What's Changed

  • Add an iOS demo app with UI tests and an iOS CI job by @0xLeif in #159

Added

  • Application.didFailToPersist(_:), an overridable method that is called when a FileState file cannot be decoded, written or deleted, or when a StoredState or SyncState value cannot be encoded. It receives an Application.PersistenceFailure with the kind of state, the operation, the state's key and the underlying error. The default implementation does nothing, and logging is unchanged, so an app that does not override it behaves as before. A FileState that was never written is not reported. An unreadable file is reported once, not on every read.
  • An iOS demo app in Examples/DemoApp with UI tests, and an iOS CI job that runs them and the unit tests inside the app.

Fixed

  • A write to a State or FileState made right after its first read is no longer undone. Outside XCTest on Apple platforms the getter filled the cache from a deferred task, which ran after the write and put the older value back. The cache is now filled before the getter returns.
  • state(initial:) evaluates its initial value once per scope, as documented, instead of on every access. A state with a UUID() or Date() initial now reads the same value every time, and reset() restores that first value.
  • A write through @AppState, @StoredState, @FileState, @SecureState, @SyncState, @Slice or @OptionalSlice on an ObservableObject that declares its own objectWillChange is stored. It used to be dropped.
  • StoredState no longer crashes with NSInvalidArgumentException on a value that JSONEncoder rejects, such as a struct holding Double.nan. The write is logged and ignored, and the previous value stays.
  • A FileState created with isBase64Encoded: false reads back values whose JSON text is also valid Base64, such as true or 1234. They used to read as the initial value after a relaunch.
  • Application.promote(to:) keeps the promoted application registered for iCloud key-value store changes when icloudStore was loaded before the promotion.
  • FileManager.defaultFileStatePath is no longer percent-encoded. A documents path with a space or a non-ASCII character pointed at a directory that does not exist.
  • Overrides of the same dependency can end in any order. Ending an older override while a newer one was alive used to put the older value back at once, and left a stale override in place after both had ended. The dependency is now the newest override still alive, and the original once none is.

Changed

  • Releasing a DependencyOverride restores the dependency, as the override documentation says. Before, only cancel() did. Code that discarded the token and relied on the override staying must now keep the token, or use Application.promote(_:with:) for a permanent replacement.

Documentation

  • The restricted state example in Advanced Usage keeps its ID in a constant. With UUID() inside the computed property the state never kept a value.
  • The dependency example in State and Dependency Usage compiles in Swift 6 mode.

Full Changelog: 3.0.2...3.1.0

AppState

macOS Build Ubuntu Build Windows Build License Version

Read this in other languages: French | German | Hindi | Portuguese | Russian | Simplified Chinese | Spanish

AppState is a Swift 6 library for managing application state in a thread-safe, type-safe, and SwiftUI-friendly way. Centralize and synchronize state across your app; inject dependencies anywhere.

Requirements

  • iOS: 17.0+
  • watchOS: 10.0+
  • macOS: 14.0+
  • tvOS: 17.0+
  • visionOS: 1.0+
  • Swift: 6.0+
  • Xcode: 16.0+

Non-Apple Platform Support: Linux & Windows

๐ŸŽ Features marked with this symbol are specific to Apple platforms, as they rely on Apple technologies such as iCloud and the Keychain.

Key Features

AppState includes:

  • State: Centralized state management that allows you to encapsulate and broadcast changes across the app.
  • StoredState: Persistent state using UserDefaults, ideal for saving small amounts of data between app launches.
  • FileState: Persistent state stored using FileManager, useful for storing larger amounts of data securely on disk.
  • ๐ŸŽ SwiftData (ModelState): Manage SwiftData @Model objects through AppState by injecting a shared ModelContainer and reading/writing models with ModelState.
  • ๐ŸŽ SyncState: Synchronize state across multiple devices using iCloud, ensuring consistency in user preferences and settings.
  • ๐ŸŽ SecureState: Store sensitive data securely using the Keychain, protecting user information such as tokens or passwords.
  • Dependency Management: Inject dependencies like network services or database clients across your app for better modularity and testing.
  • Slicing: Access specific parts of a state or dependency for granular control without needing to manage the entire application state.
  • Constants: Access read-only slices of your state when you need immutable values.
  • Observed Dependencies: Observe ObservableObject dependencies so your views update when they change.

Getting Started

Add AppState via Swift Package Manager โ€” see the Installation Guide. Then check the Usage Overview for a quick introduction.

Quick Example

import AppState
import SwiftUI

private extension Application {
    var counter: State<Int> {
        state(initial: 0)
    }
}

struct ContentView: View {
    @AppState(\.counter) var counter: Int

    var body: some View {
        VStack {
            Text("Count: \(counter)")
            Button("Increment") { counter += 1 }
        }
    }
}

Documentation

Hereโ€™s a detailed breakdown of AppState's documentation:

  • Installation Guide: How to add AppState to your project using Swift Package Manager.
  • Usage Overview: An overview of key features with example implementations.

Detailed Usage Guides:

Contributing

We welcome contributions! Please check out our Contributing Guide for how to get involved.

Next Steps

Start with the Usage Overview. For Just-In-Time creation and preloading, see the Advanced Usage Guide. The Constant and ObservedDependency guides cover additional features.

Description

  • Swift Tools 6.0.0
View More Packages from this Author

Dependencies

Last updated: Sun Nov 22 2026 00:27:04 GMT-1000 (Hawaii-Aleutian Standard Time)