Skip to content

fatbobman/PersistentHistoryTrackingKit

Repository files navigation

PersistentHistoryTrackingKit

A Swift package that processes Core Data persistent history so apps and extensions can observe, merge, and clean transactions consistently.

Swift Platforms License DeepWiki

English | 中文

Motivation

Core Data persistent history records changes made by an app, its extensions, background contexts, and optional CloudKit synchronization. Turning those records into a reliable merge and cleanup flow requires coordinating authors, contexts, and lifecycle events.

PersistentHistoryTrackingKit provides that coordination with an actor-based processing pipeline and hooks for app-specific observation or merge behavior. For the underlying Core Data model, read Using Persistent History Tracking in Core Data.

Features

  • Processes persistent-history transactions from configured authors and merges them into chosen contexts.
  • Provides Observer Hooks for monitoring entity operations and Merge Hooks for custom merge pipelines.
  • Supports automatic, manual, or disabled history cleanup and optional CloudKit mirroring authors.
  • Uses Swift concurrency throughout and exposes explicit start/stop lifecycle control.

Quick Start

Add PersistentHistoryTrackingKit with Swift Package Manager:

dependencies: [
    .package(url: "https://github.com/fatbobman/PersistentHistoryTrackingKit.git", from: "2.1.0")
]

Enable persistent history and create the kit with the authors that share the store:

import CoreData
import PersistentHistoryTrackingKit

let container = NSPersistentContainer(name: "Model")
let description = container.persistentStoreDescriptions.first!
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
container.loadPersistentStores { _, error in
    if let error { fatalError("Failed to load store: \(error)") }
}

container.viewContext.transactionAuthor = "MainApp"
let kit = PersistentHistoryTrackingKit(
    container: container,
    currentAuthor: "MainApp",
    allAuthors: ["MainApp", "WidgetExtension"],
    userDefaults: .standard
)

Documentation

Contributing & Testing

Contributions are welcome through pull requests. Run the full suite with ./test.sh; it enables Core Data concurrency assertions and parallel execution.

License

PersistentHistoryTrackingKit is available under the MIT license. See the LICENSE file for more info.

Author

Fatbobman (肘子) — Blog: fatbobman.com · X: @fatbobman

Support

If this project helps you, please consider supporting my work:

About

A Swift package that processes Core Data persistent history so apps and extensions can observe, merge, and clean transactions consistently.

Topics

Resources

License

Stars

129 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors