Implementing a background delivery extension
At a glance
| Item | Summary |
|---|---|
| Purpose | Receive up-to-date financial data in your app and its extensions by adding a background delivery extension. |
| App architecture | A Swift sample with the source-visible chain FinanceKitBackgroundDeliveryApp → ContentView → TransactionModel → SpendingWidgetProvider → FinanceKit APIs. |
| Main patterns | No named application pattern supported by the extracted structure |
| Project style | 7 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: async declaration or closure, Sendable or @Sendable; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper. |
| Key frameworks/packages | FinanceKit, Foundation, SwiftUI, WidgetKit; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── SpendingTracker/
│ ├── SpendingTracker/
│ │ ├── FinanceKitBackgroundDeliveryApp.swift
│ │ ├── ContentView.swift
│ │ ├── Info.plist
│ │ └── SpendingTracker.entitlements
│ ├── SpendingTrackerWidgetExtension/
│ │ └── SpendingWidget.swift
│ ├── SpendingTrackerBackgroundDeliveryExtension/
│ │ └── SpendingBackgroundDeliveryExtension.swift
│ ├── Shared/
│ │ ├── FinanceUtilities.swift
│ │ ├── Storage.swift
│ │ └── FormattingUtilities.swift
│ └── SpendingTracker.xcodeproj/
│ ├── .xcodesamplecode.plist
│ └── project.pbxproj
└── Configuration/
└── SampleCode.xcconfig
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: Swift.
- The verified tree contains 9 project/configuration file(s) and 11 source declaration(s).
Overall architecture
flowchart LR
N1["FinanceKitBackgroundDeliveryApp"]
N2["ContentView"]
N3["TransactionModel"]
N4["SpendingWidgetProvider"]
N5["FinanceKit APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
Reference code
SpendingTracker/SpendingTracker/FinanceKitBackgroundDeliveryApp.swift:10 — architecture anchor
@main
struct FinanceKitBackgroundDeliveryApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}Interpretation
The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into FinanceKit.
Ownership and state
classDiagram
SpendingWidget *-- String : kind
SpendingEntry *-- Date : date
SpendingEntry o-- Decimal : total
SpendingWidgetView o-- SpendingEntry : entry
Ownership evidence
SpendingTracker/SpendingTrackerWidgetExtension/SpendingWidget.swift:13 — stored dependency or nearest verified ownership anchor
@main
struct SpendingWidget: Widget {
let kind: String = "com.myapp.spending-widget"
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
SpendingWidget |
String (kind) |
owns value state | Initialized by the owner; the binding is immutable |
SpendingEntry |
Date (date) |
owns value state | Initialized by the owner; the binding is immutable |
SpendingEntry |
Decimal (total) |
stores or receives | Initialized by the owner; the binding is immutable |
SpendingWidgetView |
SpendingEntry (entry) |
stores or receives | App/module collaborators |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
| Concern | Source mechanism | Verified placement or handoff | Evidence |
|---|---|---|---|
| Suspension boundary | async declaration or closure |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | SpendingTracker/Shared/FinanceUtilities.swift:15 |
| Transfer contract | Sendable or @Sendable |
The source declares a sendability boundary; this alone does not synchronize mutable state. | SpendingTracker/SpendingTrackerWidgetExtension/SpendingWidget.swift:35 |
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
Reference code
SpendingTracker/Shared/FinanceUtilities.swift:15 — representative execution boundary
static func calculateWeeklySpendingTotal() async throws -> Decimal {
// ...
let accounts = try await FinanceStore.shared.accounts(query: AccountQuery())
// ...
}State propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | SpendingTracker/SpendingTracker/ContentView.swift:13 |
| Source import | FinanceKit |
The cited file imports this module; runtime use and architectural role are not inferred. | SpendingTracker/Shared/FinanceUtilities.swift:8 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | SpendingTracker/Shared/FinanceUtilities.swift:9 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | SpendingTracker/SpendingTracker/ContentView.swift:9 |
| Source import | WidgetKit |
The cited file imports this module; runtime use and architectural role are not inferred. | SpendingTracker/SpendingTracker/ContentView.swift:10 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
SpendingTracker/SpendingTracker/FinanceKitBackgroundDeliveryApp.swift:11 — representative type boundary
@main
struct FinanceKitBackgroundDeliveryApp: App {
// ...
ContentView()
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
FinanceKitBackgroundDeliveryApp |
Application entry and top-level composition | App |
SpendingWidgetProvider |
Supplies a capability or framework resource | TimelineProvider |
SpendingWidgetView |
User-interface presentation and input forwarding | View |
ContentView |
User-interface presentation and input forwarding | View |
TransactionModel |
Feature data or observable state | Identifiable |
SpendingWidget |
Represents a feature value or composable behavior | Widget |
SpendingEntry |
Represents a feature value or composable behavior | TimelineEntry |
SpendingBackgroundDeliveryExtension |
Represents a feature value or composable behavior | BackgroundDeliveryExtension |
TransactionRow |
Represents a feature value or composable behavior | View |
FinanceUtilities |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
appGroupIdentifier (SpendingTracker/Shared/Storage.swift:12) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
weeklySpendingKey (SpendingTracker/Shared/Storage.swift:13) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
defaults (SpendingTracker/Shared/Storage.swift:15) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
storage (SpendingTracker/SpendingTracker/ContentView.swift:16) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
Reference code
SpendingTracker/Shared/Storage.swift:12 — representative boundary
struct Storage {
// This is the identifier of the app group in the project settings.
private static var appGroupIdentifier = "group.com.example.apple-samplecode.FinanceKit-background-delivery"
private static var weeklySpendingKey = "WeeklySpending"
private let defaults: UserDefaults
init() {
guard let defaults = UserDefaults(suiteName: Self.appGroupIdentifier) else {
fatalError("Couldn't initialize UserDefaults for app group")
}
self.defaults = defaults
}
var weeklySpending: Decimal {
// Read the value as a `String` and convert it to a `Decimal` if it's not `nil`.
if let amount = defaults.string(forKey: Self.weeklySpendingKey).flatMap({ Decimal(string: $0) }) {
return amount
} else {
return 0
}
}
func setWeeklySpending(_ amount: Decimal) {
// Convert the `Decimal` value into a `String` before storing it.
defaults.set(String(describing: amount), forKey: Self.weeklySpendingKey)
}
}Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Application entry and top-level composition | FinanceKitBackgroundDeliveryApp |
The source’s App suffix makes this role explicit. |
| Feature data or observable state | TransactionModel |
The source’s Model suffix makes this role explicit. |
| Supplies a capability or framework resource | SpendingWidgetProvider |
The source’s Provider suffix makes this role explicit. |
| User-interface presentation and input forwarding | ContentView, SpendingWidgetView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| No named application pattern | SpendingTracker/SpendingTracker/FinanceKitBackgroundDeliveryApp.swift:10 |
The verified source directly composes concrete framework types; this document avoids forcing a pattern name. |
Naming conventions
- Types: App: FinanceKitBackgroundDeliveryApp; Model: TransactionModel; Provider: SpendingWidgetProvider; View: ContentView, SpendingWidgetView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
getSnapshot,getTimeline,placeholder,didReceiveData,willTerminate,updateView,updateTransactions,updateWeeklySpending. - Files:
SpendingTracker/SpendingTracker/FinanceKitBackgroundDeliveryApp.swift,SpendingTracker/SpendingTrackerWidgetExtension/SpendingWidget.swift,SpendingTracker/SpendingTrackerBackgroundDeliveryExtension/SpendingBackgroundDeliveryExtension.swift,SpendingTracker/SpendingTracker/ContentView.swift,SpendingTracker/Shared/FinanceUtilities.swift,SpendingTracker/Shared/Storage.swift.
Architecture takeaways
FinanceKitBackgroundDeliveryAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches FinanceKit, SwiftUI, WidgetKit through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- The source does not justify labeling the design protocol-oriented.
Source map
| Source file | Relevant symbols |
|---|---|
SpendingTracker/SpendingTracker/FinanceKitBackgroundDeliveryApp.swift |
Cited implementation, FinanceKitBackgroundDeliveryApp |
SpendingTracker/SpendingTrackerWidgetExtension/SpendingWidget.swift |
Cited implementation, Sendable or @Sendable, SpendingWidget, SpendingEntry, SpendingWidgetProvider, SpendingWidgetView |
SpendingTracker/Shared/Storage.swift |
Cited implementation, Storage |
SpendingTracker/SpendingTracker/ContentView.swift |
Cited implementation, SwiftUI state property wrapper, SwiftUI, WidgetKit, ContentView, TransactionRow, TransactionModel |
SpendingTracker/Shared/FinanceUtilities.swift |
async declaration or closure, FinanceKit, Foundation, FinanceUtilities |
SpendingTracker/SpendingTrackerBackgroundDeliveryExtension/SpendingBackgroundDeliveryExtension.swift |
SpendingBackgroundDeliveryExtension |
SpendingTracker/Shared/FormattingUtilities.swift |
Feature implementation |