Sharing Core Data objects between iCloud users
At a glance
| Item | Summary |
|---|---|
| Purpose | Use Core Data and CloudKit to synchronize data between devices of an iCloud user and share data between different iCloud users. |
| App architecture | A Swift sample bundle with entry-bearing project variants CoreDataCloudKitShare, CoreDataCloudKitShareOnWatch, each leading to CoreData APIs. |
| Main patterns | View-controller organization, Delegate or data-source callbacks, Binding-based state propagation |
| Project style | 29 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: await suspension point, DispatchQueue.main.async; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: ObservableObject, NotificationCenter, SwiftUI state property wrapper, receive(on:). |
| Key frameworks/packages | CoreData, CloudKit, SwiftUI, Foundation, UIKit; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── CoreDataCloudKitShare/
│ ├── CoreDataCloudKitShareApp.swift
│ └── SceneDelegate.swift
├── CoreDataCloudKitShareOnWatch/
│ ├── CoreDataCloudKitShareApp.swift
│ └── AppDelegate.swift
├── Persistence/
│ └── PersistenceController.swift
└── SwiftUI/
├── ParticipantView.swift
├── SwiftUIHelper.swift
├── TaggingView.swift
├── PhotoGridView.swift
├── RatingView.swift
├── AddToExistingShareView.swift
└── FullImageView.swift
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 8 project/configuration file(s) and 35 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["CoreDataCloudKitShare"]
V2["CoreDataCloudKitShareOnWatch"]
Boundary["CoreData APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
CoreDataCloudKitShare/CoreDataCloudKitShareApp.swift:11 — architecture anchor
@main
struct CoreDataCloudKitShareApp: App {
@ApplicationDelegateAdaptor var appDelegate: AppDelegate
private let persistentContainer = PersistenceController.shared.persistentContainer
var body: some Scene {
#if InitializeCloudKitSchema
WindowGroup {
Text("Initializing CloudKit Schema...").font(.title)
Text("Stop after Xcode says 'no more requests to execute', " +
"then check with CloudKit Console if the schema is created correctly.").padding()
}
#else
WindowGroup {
PhotoGridView()
.environment(\.managedObjectContext, persistentContainer.viewContext)
}
#endif
}
}Interpretation
The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.
Ownership and state
classDiagram
CoreDataCloudKitShareApp o-- AppDelegate : appDelegate
PersistenceController o-- Notification : cdcksStoreDidChange
PersistenceController *-- PersistenceController : shared
PersistenceController o-- NSPersistentCloudKitContainer : persistentContainer
Ownership evidence
CoreDataCloudKitShare/CoreDataCloudKitShareApp.swift:13 — stored dependency or nearest verified ownership anchor
@main
struct CoreDataCloudKitShareApp: App {
@ApplicationDelegateAdaptor var appDelegate: AppDelegate
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
CoreDataCloudKitShareApp |
AppDelegate (appDelegate) |
stores or receives | App/module collaborators |
PersistenceController |
Notification (cdcksStoreDidChange) |
stores or receives | Initialized by the owner; the binding is immutable |
PersistenceController |
PersistenceController (shared) |
creates and retains | Initialized by the owner; the binding is immutable |
PersistenceController |
NSPersistentCloudKitContainer (persistentContainer) |
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 | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | Persistence/PersistenceController+Photo.swift:138 |
| Queue scheduling | DispatchQueue.main.async |
The source addresses the main dispatch queue. | Persistence/PersistenceController.swift:111 |
@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
Persistence/PersistenceController+Photo.swift:138 — representative execution boundary
#if os(iOS) || os(macOS)
let photo = await persistentContainer.viewContext.perform {
let coordinator = persistentContainer.viewContext.persistentStoreCoordinator
// ...
}
#endifState 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. | CoreDataCloudKitShare/AppDelegate_iOS.swift:11 |
| State propagation | NotificationCenter |
NotificationCenter distributes named process-local events. | Persistence/PersistenceController+History.swift:72 |
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | SwiftUI/AddToExistingShareView.swift:13 |
| Combine scheduling | receive(on:) |
receive(on:) selects the scheduler for downstream delivery. |
SwiftUI/SwiftUIHelper.swift:13 |
| Source import | CoreData |
The cited file imports this module; runtime use and architectural role are not inferred. | CoreDataCloudKitShare/AppDelegate_iOS.swift:8 |
| Source import | CloudKit |
The cited file imports this module; runtime use and architectural role are not inferred. | CoreDataCloudKitShare/AppDelegate_macOS.swift:9 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | CoreDataCloudKitShare/CoreDataCloudKitShareApp.swift:8 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Persistence/CloudKitHelper.swift:8 |
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
CoreDataCloudKitShare/CoreDataCloudKitShareApp.swift:12 — representative type boundary
@main
struct CoreDataCloudKitShareApp: App {
@ApplicationDelegateAdaptor var appDelegate: AppDelegate
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
CoreDataCloudKitShareApp |
Application entry and top-level composition | App |
CoreDataCloudKitShareApp |
Application entry and top-level composition | App |
PersistenceController |
View lifecycle, callbacks, and feature coordination | NSObject, ObservableObject |
ParticipantView |
User-interface presentation and input forwarding | View |
TaggingView |
User-interface presentation and input forwarding | View |
PhotoGridView |
User-interface presentation and input forwarding | View |
RatingView |
User-interface presentation and input forwarding | View |
SceneDelegate |
Receives callback-driven events | UIResponder, UIWindowSceneDelegate |
AppDelegate |
Receives callback-driven events | NSObject, WKApplicationDelegate |
AddToExistingShareView |
User-interface presentation and input forwarding | View |
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 |
|---|---|---|---|
persistentContainer (CoreDataCloudKitShare/CoreDataCloudKitShareApp.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. |
deduplicate (Persistence/PersistenceController+Deduplicate.swift:65) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: hide an implementation step that is not part of the collaboration surface. |
markAsDeduplicated (Persistence/PersistenceController+Deduplicate.swift:113) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: hide an implementation step that is not part of the collaboration surface. |
removeDeduplicatedTags (Persistence/PersistenceController+Deduplicate.swift:188) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: hide an implementation step that is not part of the collaboration surface. |
Reference code
CoreDataCloudKitShare/CoreDataCloudKitShareApp.swift:15 — representative boundary
@main
struct CoreDataCloudKitShareApp: App {
// ...
private let persistentContainer = PersistenceController.shared.persistentContainer
// ...
}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 | CoreDataCloudKitShareApp |
The source’s App suffix makes this role explicit. |
| View lifecycle, callbacks, and feature coordination | PersistenceController |
The source’s Controller suffix makes this role explicit. |
| Receives callback-driven events | AppDelegate, SceneDelegate |
The source’s Delegate suffix makes this role explicit. |
| User-interface presentation and input forwarding | AddToExistingShareView, FullImageView, ManagingSharesView, ParticipantView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | Persistence/PersistenceController.swift:31 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Delegate or data-source callbacks | CoreDataCloudKitShare/AppDelegate_iOS.swift:11 |
Callback protocols invert event delivery back into the sample’s owner. |
| Binding-based state propagation | SwiftUI/AddToExistingShareView.swift:13 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
Naming conventions
- Types: App: CoreDataCloudKitShareApp; Controller: PersistenceController; Delegate: AppDelegate, SceneDelegate; View: AddToExistingShareView, FullImageView, ManagingSharesView, ParticipantView, PhotoGridItemView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
participantListView,sectionHeader,sectionContent,toolbarItems,purgeShare,deleteParticipant,processStoreChangeNotification,addParticipant. - Files:
CoreDataCloudKitShare/CoreDataCloudKitShareApp.swift,CoreDataCloudKitShareOnWatch/CoreDataCloudKitShareApp.swift,Persistence/PersistenceController.swift,SwiftUI/ParticipantView.swift,SwiftUI/TaggingView.swift,SwiftUI/PhotoGridView.swift.
Architecture takeaways
CoreDataCloudKitShareAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches CoreData, CloudKit, SwiftUI, 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 |
|---|---|
CoreDataCloudKitShare/CoreDataCloudKitShareApp.swift |
Cited implementation, CoreDataCloudKitShareApp, SwiftUI |
Persistence/PersistenceController+Deduplicate.swift |
Cited implementation, Feature implementation |
Persistence/PersistenceController.swift |
PersistenceController, DispatchQueue.main.async, UserInfoKey, TransactionAuthor |
CoreDataCloudKitShare/AppDelegate_iOS.swift |
Cited implementation, ObservableObject, CoreData, AppDelegate |
SwiftUI/AddToExistingShareView.swift |
Cited implementation, SwiftUI state property wrapper, AddToExistingShareView |
Persistence/PersistenceController+Photo.swift |
await suspension point, Feature implementation |
Persistence/PersistenceController+History.swift |
NotificationCenter, Feature implementation |
SwiftUI/SwiftUIHelper.swift |
receive(on:), Layout, MenuButtonLabel, WatchMenuLabelStyle, SheetToolbarItemLabel, EmptyListModifier, IconOnlyButton, ClearableTextField |
CoreDataCloudKitShare/AppDelegate_macOS.swift |
CloudKit, AppDelegate |
Persistence/CloudKitHelper.swift |
Foundation, Feature implementation |
CoreDataCloudKitShareOnWatch/CoreDataCloudKitShareApp.swift |
CoreDataCloudKitShareApp |
SwiftUI/ParticipantView.swift |
ParticipantView, ParticipantListHeader, Participant |
SwiftUI/TaggingView.swift |
TaggingView, FilteredTagList, TagListHeader |
SwiftUI/PhotoGridView.swift |
ActiveSheet, PhotoGridView |
SwiftUI/RatingView.swift |
RatingView, RatingListHeader |
CoreDataCloudKitShare/SceneDelegate.swift |
SceneDelegate |