Monitoring location changes with Core Location
At a glance
| Item | Summary |
|---|---|
| Purpose | Define boundaries and act on user location updates. |
| App architecture | A Swift sample with the source-visible chain LocationMonitorSampleApp → ContentView → ObservableMonitorModel → CoreLocation APIs. |
| Main patterns | Delegate or data-source callbacks, Publisher-backed observable state |
| Project style | 3 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: @MainActor, Task, await suspension point; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: ObservableObject, @Published, SwiftUI state property wrapper. |
| Key frameworks/packages | SwiftUI, CoreLocation, Foundation, OSLog, UIKit; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── LocationMonitorSampleApp/
│ ├── LocationMonitorSampleApp.swift
│ ├── ContentView.swift
│ ├── AppDelegate.swift
│ └── Info.plist
├── Configuration/
│ └── SampleCode.xcconfig
└── LocationMonitorSampleApp.xcodeproj/
├── .xcodesamplecode.plist
└── project.pbxproj
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 4 project/configuration file(s) and 5 source declaration(s).
Overall architecture
flowchart LR
N1["LocationMonitorSampleApp"]
N2["ContentView"]
N3["ObservableMonitorModel"]
N4["CoreLocation APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
LocationMonitorSampleApp/LocationMonitorSampleApp.swift:10 — architecture anchor
@main
struct LocationMonitorSampleApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
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 Core Location.
Ownership and state
classDiagram
ContentView *-- CLLocationCoordinate2D : appleParkLocation
ContentView *-- UUID : testBeaconId
ObservableMonitorModel *-- ObservableMonitorModel : shared
ObservableMonitorModel o-- CLMonitor : monitor
Ownership evidence
LocationMonitorSampleApp/ContentView.swift:12 — stored dependency or nearest verified ownership anchor
let appleParkLocation = CLLocationCoordinate2D(latitude: 37.3346, longitude: -122.0090)| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
ContentView |
CLLocationCoordinate2D (appleParkLocation) |
creates and retains | Initialized by the owner; the binding is immutable |
ContentView |
UUID (testBeaconId) |
owns value state | Initialized by the owner; the binding is immutable |
ObservableMonitorModel |
ObservableMonitorModel (shared) |
creates and retains | Initialized by the owner; the binding is immutable |
ObservableMonitorModel |
CLMonitor (monitor) |
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 |
|---|---|---|---|
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | LocationMonitorSampleApp/ContentView.swift:30 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | LocationMonitorSampleApp/ContentView.swift:58 |
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | LocationMonitorSampleApp/ContentView.swift:59 |
@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
LocationMonitorSampleApp/ContentView.swift:30 — representative execution boundary
@MainActor
public class ObservableMonitorModel: ObservableObject {
// ...
static let shared = ObservableMonitorModel()
// ...
}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 | ObservableObject |
ObservableObject supplies an observation contract. | LocationMonitorSampleApp/ContentView.swift:31 |
| State propagation | @Published |
A published property can emit owner-controlled changes. | LocationMonitorSampleApp/ContentView.swift:39 |
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | LocationMonitorSampleApp/ContentView.swift:115 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | LocationMonitorSampleApp/ContentView.swift:8 |
| Source import | CoreLocation |
The cited file imports this module; runtime use and architectural role are not inferred. | LocationMonitorSampleApp/ContentView.swift:9 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | LocationMonitorSampleApp/AppDelegate.swift:8 |
| Source import | OSLog |
The cited file imports this module; runtime use and architectural role are not inferred. | LocationMonitorSampleApp/AppDelegate.swift:9 |
| Source import | UIKit |
The cited file imports this module; runtime use and architectural role are not inferred. | LocationMonitorSampleApp/AppDelegate.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
LocationMonitorSampleApp/LocationMonitorSampleApp.swift:11 — representative type boundary
@main
struct LocationMonitorSampleApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
LocationMonitorSampleApp |
Application entry and top-level composition | App |
ObservableMonitorModel |
Feature data or observable state | ObservableObject |
ContentView |
User-interface presentation and input forwarding | View |
ErrorView |
User-interface presentation and input forwarding | View |
AppDelegate |
Receives callback-driven events | NSObject, UIApplicationDelegate |
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 |
|---|---|---|---|
monitor (LocationMonitorSampleApp/ContentView.swift:38) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
authSession (LocationMonitorSampleApp/ContentView.swift:48) |
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. |
notificationCenter (LocationMonitorSampleApp/ContentView.swift:49) |
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. |
notificationContent (LocationMonitorSampleApp/ContentView.swift:50) |
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
LocationMonitorSampleApp/ContentView.swift:38 — representative boundary
@MainActor
public class ObservableMonitorModel: ObservableObject {
// ...
public var monitor: CLMonitor?
// ...
}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 | LocationMonitorSampleApp |
The source’s App suffix makes this role explicit. |
| Receives callback-driven events | AppDelegate |
The source’s Delegate suffix makes this role explicit. |
| Feature data or observable state | ObservableMonitorModel |
The source’s Model suffix makes this role explicit. |
| User-interface presentation and input forwarding | ContentView, ErrorView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Delegate or data-source callbacks | LocationMonitorSampleApp/AppDelegate.swift:12 |
Callback protocols invert event delivery back into the sample’s owner. |
| Publisher-backed observable state | LocationMonitorSampleApp/ContentView.swift:39 |
Published properties notify observers while mutation remains with the state object. |
Main application flow
One launch starts a long-lived monitor task that registers conditions, restores their last events, and then reduces each asynchronous event into observable UI rows.
sequenceDiagram
participant System as UIApplication
participant App as AppDelegate
participant Model as ObservableMonitorModel
participant Monitor as CLMonitor
participant UI as ContentView
System-->>App: didFinishLaunching
App->>Model: startMonitoringConditions()
Model->>Monitor: Create named monitor
Model->>Monitor: Add geographic and beacon conditions
loop existing identifiers
Model->>Monitor: record(for: identifier)
Monitor-->>Model: lastEvent
end
loop asynchronous condition events
Monitor-->>Model: events sequence yields event
Model->>Monitor: read prior lastEvent
Model-->>UI: Update published UIRows
end
Reference code
LocationMonitorSampleApp/ContentView.swift:77 — startMonitoringConditions
func startMonitoringConditions() {
Task {
print("Set up monitor")
monitor = await CLMonitor(monitorName)
await monitor!.add(getCircularGeographicCondition(), identifier: "ApplePark")
await monitor!.add(getBeaconIdentityCondition(), identifier: "TestBeacon")
for identifier in await monitor!.identifiers {
guard let lastEvent = await monitor!.record(for: identifier)?.lastEvent else { continue }
UIRows[identifier] = [lastEvent]
}
for try await event in await monitor!.events {
// While handling the most recent event, the last event is still updating
// and shows the prior state, allowing you to reference both.
guard let lastEvent = await monitor!.record(for: event.identifier)?.lastEvent else { continue }
if event.state == lastEvent.state {
// If the event state is the same as the previous state, the only new information is in diagnostics.
// Because the event isn't a new state, don't record it in your UI.
// Because you respond to service session diagnostics, you don't need to also worry about the ones delivered by the monitor.
continue
}
UIRows[event.identifier] = [event]
UIRows[event.identifier]?.append(lastEvent)
}
}
}Naming conventions
- Types: App: LocationMonitorSampleApp; Delegate: AppDelegate; Model: ObservableMonitorModel; View: ContentView, ErrorView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
without,startAuthSession,startMonitoringConditions,updateRecords,getCircularGeographicCondition,getBeaconIdentityCondition,application. - Files:
LocationMonitorSampleApp/LocationMonitorSampleApp.swift,LocationMonitorSampleApp/ContentView.swift,LocationMonitorSampleApp/AppDelegate.swift.
Architecture takeaways
LocationMonitorSampleAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, CoreLocation, UIKit 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 |
|---|---|
LocationMonitorSampleApp/LocationMonitorSampleApp.swift |
Cited implementation, LocationMonitorSampleApp |
LocationMonitorSampleApp/ContentView.swift |
Cited implementation, @MainActor, Task, await suspension point, ObservableObject, @Published, SwiftUI state property wrapper, SwiftUI, CoreLocation, ObservableMonitorModel, ContentView, ErrorView, ContentView_Previews |
LocationMonitorSampleApp/AppDelegate.swift |
Cited implementation, Foundation, OSLog, UIKit, AppDelegate |