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. |
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 {
// ...
}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.
Class and protocol design
CoreDataCloudKitShare/CoreDataCloudKitShareApp.swift:12 — representative type boundary
struct CoreDataCloudKitShareApp: App {
// ...
}| 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 |
CoreDataCloudKitShareApp |
CoreDataCloudKitShareOnWatch/CoreDataCloudKitShareApp.swift |
CoreDataCloudKitShareApp |
Persistence/PersistenceController.swift |
UserInfoKey, TransactionAuthor, PersistenceController |
SwiftUI/ParticipantView.swift |
ParticipantView, ParticipantListHeader, Participant |
SwiftUI/SwiftUIHelper.swift |
Layout, MenuButtonLabel, WatchMenuLabelStyle, SheetToolbarItemLabel, EmptyListModifier, IconOnlyButton, ClearableTextField |
SwiftUI/TaggingView.swift |
TaggingView, FilteredTagList, TagListHeader |
SwiftUI/PhotoGridView.swift |
ActiveSheet, PhotoGridView |
SwiftUI/RatingView.swift |
RatingView, RatingListHeader |
CoreDataCloudKitShare/SceneDelegate.swift |
SceneDelegate |
CoreDataCloudKitShareOnWatch/AppDelegate.swift |
AppDelegate |