Sample CodeiOS, iPadOS, Mac CatalystReviewed 2026-07-21View on Apple Developer

Synchronizing documents in the iCloud environment

At a glance

Item Summary
Purpose Manage documents across multiple devices to create a seamless editing and collaboration experience.
App architecture A Swift sample with the source-visible chain AppDelegateDetailViewControllerDocumentMetadataProviderUIKit APIs.
Main patterns View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks
Project style 18 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: DispatchQueue.main.async, DispatchQueue.global.async, DispatchQueue(label:); none alone proves a background thread.
State/event model Source-visible mechanisms: AnyCancellable, NotificationCenter, receive(on:).
Key frameworks/packages UIKit, Foundation, Combine; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── SimpleiCloudDocument/
    ├── AppDelegate.swift
    ├── Detail/
    │   ├── Document.swift
    │   ├── DetailViewController.swift
    │   ├── ImageViewController.swift
    │   ├── DetailViewController+CollectionView.swift
    │   └── ImageCVCell.swift
    ├── Main/
    │   ├── MainViewController.swift
    │   ├── MetadataProvider.swift
    │   ├── MainViewController+Document.swift
    │   ├── MainViewController+TableView.swift
    │   └── DiffableMetadataSource.swift
    └── SceneDelegate.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 7 project/configuration file(s) and 17 source declaration(s).

Overall architecture

Reference code

SimpleiCloudDocument/AppDelegate.swift:10 — architecture anchor

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        return true
    }
    // ...
}

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 UIKit.

Ownership and state

Ownership evidence

SimpleiCloudDocument/Detail/Document.swift:36 — stored dependency or nearest verified ownership anchor

    private lazy var accessQueue: DispatchQueue = {
        return DispatchQueue(label: "Document", attributes: .concurrent)
    }()
Owner Object or state Relationship Mutation authority
Changes DispatchQueue (accessQueue) stores or receives Owning lexical scope
Changes Dictionary (_fileWrappersUnderRoot) owns value state Owning lexical scope
Changes Changes (_unsavedUserChanges) creates and retains Owning lexical scope
Changes Changes (unpresentedPeerChanges) creates and retains Owning type writes; wider scope can read

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
Queue scheduling DispatchQueue.main.async The source addresses the main dispatch queue. SimpleiCloudDocument/Detail/DetailViewController+CollectionView.swift:28
Queue scheduling DispatchQueue.global.async The source addresses a global dispatch queue; no stable thread identity is implied. SimpleiCloudDocument/Detail/DetailViewController+Conflict.swift:56
Queue scheduling DispatchQueue(label:) The source constructs a dispatch queue; its label alone does not prove a thread. SimpleiCloudDocument/Detail/Document.swift:37

@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

SimpleiCloudDocument/Detail/DetailViewController+CollectionView.swift:28 — representative execution boundary

            DispatchQueue.main.async {
                // ...
                    self.presentImageViewController(image: image)
                // ...
            }

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 AnyCancellable A cancellable value records subscription lifetime management. SimpleiCloudDocument/Detail/DetailViewController.swift:13
State propagation NotificationCenter NotificationCenter distributes named process-local events. SimpleiCloudDocument/Detail/DetailViewController.swift:129
Combine scheduling receive(on:) receive(on:) selects the scheduler for downstream delivery. SimpleiCloudDocument/Detail/DetailViewController.swift:124
Source import UIKit The cited file imports this module; runtime use and architectural role are not inferred. SimpleiCloudDocument/AppDelegate.swift:8
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. SimpleiCloudDocument/Detail/DetailViewController+DocumentState.swift:8
Source import Combine The cited file imports this module; runtime use and architectural role are not inferred. SimpleiCloudDocument/Detail/DetailViewController.swift:9

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

SimpleiCloudDocument/Detail/ImageCVCell.swift:10 — representative type boundary

protocol ImagCVCellDelegate: AnyObject {
    func deleteCell(_ cell: UICollectionViewCell)
}
Type Responsibility Depends on or conforms to
AppDelegate Receives callback-driven events UIResponder, UIApplicationDelegate
ImagCVCellDelegate Defines a capability or collaboration contract AnyObject
Document Owns document data or lifecycle behavior UIDocument
MainViewController View lifecycle, callbacks, and feature coordination UITableViewController
MetadataProvider Supplies a capability or framework resource Concrete collaborators/imported frameworks
DetailViewController View lifecycle, callbacks, and feature coordination UICollectionViewController
ImageViewController View lifecycle, callbacks, and feature coordination UIViewController
SceneDelegate Receives callback-driven events UIResponder, UIWindowSceneDelegate
Changes Represents a feature value or composable behavior Concrete collaborators/imported frameworks
SegueID Represents a feature value or composable behavior Concrete collaborators/imported frameworks

The source explicitly defines local protocol relationships: DetailViewControllerImagCVCellDelegate.

Access control

Symbol Access Verified effect Likely rationale
retrieveAndPresentImage (SimpleiCloudDocument/Detail/DetailViewController+CollectionView.swift:26) 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.
presentImagePicker (SimpleiCloudDocument/Detail/DetailViewController+CollectionView.swift:40) 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.
presentImageViewController (SimpleiCloudDocument/Detail/DetailViewController+CollectionView.swift:48) 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.
resolveConflictsAsynchronously (SimpleiCloudDocument/Detail/DetailViewController+Conflict.swift:55) 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

SimpleiCloudDocument/Detail/DetailViewController+CollectionView.swift:26 — representative boundary

    private func retrieveAndPresentImage(with imageName: String) {
        // ...
                    self.presentImageViewController(image: image)
        // ...
    }

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
View lifecycle, callbacks, and feature coordination DetailViewController, ImageViewController, MainViewController The source’s Controller suffix makes this role explicit.
Receives callback-driven events AppDelegate, ImagCVCellDelegate, SceneDelegate The source’s Delegate suffix makes this role explicit.
Owns document data or lifecycle behavior Document The source’s Document suffix makes this role explicit.
Supplies a capability or framework resource MetadataProvider The source’s Provider suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
View-controller organization SimpleiCloudDocument/Detail/DetailViewController.swift:11 A controller is the verified coordination boundary; this is MVC-style only where a separate model is present.
Protocol-oriented abstraction SimpleiCloudDocument/Detail/DetailViewController+CollectionView.swift:60 A local protocol and concrete conformance create an explicit capability boundary.
Delegate or data-source callbacks SimpleiCloudDocument/AppDelegate.swift:11 Callback protocols invert event delivery back into the sample’s owner.

Naming conventions

  • Types: Controller: DetailViewController, ImageViewController, MainViewController; Delegate: AppDelegate, ImagCVCellDelegate, SceneDelegate; Document: Document; Provider: MetadataProvider.
  • Protocols: ImagCVCellDelegate.
  • Methods: application, clear, load, contents, save, revert, accommodatePresentedItemDeletion, handleError.
  • Files: SimpleiCloudDocument/AppDelegate.swift, SimpleiCloudDocument/Detail/Document.swift, SimpleiCloudDocument/Main/MainViewController.swift, SimpleiCloudDocument/Main/MetadataProvider.swift, SimpleiCloudDocument/Detail/DetailViewController.swift, SimpleiCloudDocument/Detail/ImageViewController.swift.

Architecture takeaways

  • AppDelegate is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches 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.
  • Local protocol relationships provide an explicit substitution boundary.

Source map

Source file Relevant symbols
SimpleiCloudDocument/AppDelegate.swift Cited implementation, UIKit, AppDelegate
SimpleiCloudDocument/Detail/Document.swift Cited implementation, DispatchQueue(label:), Document, Changes
SimpleiCloudDocument/Detail/ImageCVCell.swift ImagCVCellDelegate, ImageCVCell
SimpleiCloudDocument/Detail/DetailViewController+CollectionView.swift Cited implementation, DispatchQueue.main.async, Feature implementation
SimpleiCloudDocument/Detail/DetailViewController+Conflict.swift Cited implementation, DispatchQueue.global.async, Feature implementation
SimpleiCloudDocument/Detail/DetailViewController.swift DetailViewController, AnyCancellable, NotificationCenter, receive(on:), Combine
SimpleiCloudDocument/Detail/DetailViewController+DocumentState.swift Foundation, Feature implementation
SimpleiCloudDocument/Main/MainViewController.swift MainViewController, SegueID
SimpleiCloudDocument/Main/MetadataProvider.swift MetadataProvider, MetadataDidChangeUserInfoKey
SimpleiCloudDocument/Detail/ImageViewController.swift ImageViewController
SimpleiCloudDocument/Main/MainViewController+Document.swift Scope
SimpleiCloudDocument/SceneDelegate.swift SceneDelegate
SimpleiCloudDocument/Main/MainViewController+TableView.swift Feature implementation
SimpleiCloudDocument/Main/DiffableMetadataSource.swift MetadataItem, DiffableMetadataSource
SimpleiCloudDocument/Detail/DiffableImageSource.swift ImageItem
SimpleiCloudDocument/Detail/ImageItemsFlowLayout.swift ImageItemsFlowLayout