Building high-performance lists and collection views
At a glance
| Item | Summary |
|---|---|
| Purpose | Improve the performance of lists and collections in your app with prefetching and image preparation. |
| App architecture | A Swift sample with the source-visible chain AppDelegate → PostGridViewController → AnyModelStore → UIKit APIs. |
| Main patterns | View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks, Central store |
| Project style | 17 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: DispatchQueue(label:), DispatchQueue.main, os_unfair_lock; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: AnyCancellable, NotificationCenter, subscribe(on:), receive(on:). |
| Key frameworks/packages | UIKit, Foundation, Combine, UniformTypeIdentifiers, os; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── CollectionViewSample/
├── AppDelegate.swift
├── Stores/
│ ├── AssetStore.swift
│ └── ModelStore.swift
├── DestinationPost.swift
├── PostGridViewController.swift
├── SceneDelegate.swift
├── Utilities/
│ ├── PlaceholderStore.swift
│ ├── MemoryLimitedCache.swift
│ ├── URLSession+DownloadTaskPublisher.swift
│ └── UnfairLock.swift
└── Views/
├── DestinationPostPropertiesView.swift
└── SectionBackgroundView.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 5 project/configuration file(s) and 25 source declaration(s).
Overall architecture
flowchart LR
N1["AppDelegate"]
N2["PostGridViewController"]
N3["AnyModelStore"]
N4["UIKit APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
CollectionViewSample/AppDelegate.swift:10 — architecture anchor
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
}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
classDiagram
AssetStore *-- UIImage : placeholderFallbackImage
AssetStore *-- UnfairLock : lock
AssetStore *-- MemoryLimitedCache : preparedImages
AssetStore o-- FileBasedCache : localAssets
Ownership evidence
CollectionViewSample/Stores/AssetStore.swift:39 — stored dependency or nearest verified ownership anchor
class AssetStore: ModelStore {
static let placeholderFallbackImage = UIImage(systemName: "airplane.circle.fill")!
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
AssetStore |
UIImage (placeholderFallbackImage) |
creates and retains | Initialized by the owner; the binding is immutable |
AssetStore |
UnfairLock (lock) |
creates and retains | Initialized by the owner; the binding is immutable |
AssetStore |
MemoryLimitedCache (preparedImages) |
creates and retains | Initialized by the owner; the binding is immutable |
AssetStore |
FileBasedCache (localAssets) |
stores or receives | Initialized by the owner; the binding is immutable |
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(label:) |
The source constructs a dispatch queue; its label alone does not prove a thread. | CollectionViewSample/Stores/AssetStore.swift:46 |
| Queue scheduling | DispatchQueue.main |
The source addresses the main dispatch queue. | CollectionViewSample/Stores/AssetStore.swift:112 |
| Synchronization | os_unfair_lock |
The source references a synchronization primitive; the protected state requires surrounding review. | CollectionViewSample/Utilities/UnfairLock.swift:13 |
@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
CollectionViewSample/Stores/AssetStore.swift:46 — representative execution boundary
class AssetStore: ModelStore {
// ...
private let placeholderQueue = DispatchQueue(label: "com.apple.DestinationUnlocked.placeholderGenerationQueue",
qos: .utility,
attributes: [],
autoreleaseFrequency: .workItem,
target: nil)
// ...
}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. | CollectionViewSample/PostGridViewController.swift:20 |
| State propagation | NotificationCenter |
NotificationCenter distributes named process-local events. | CollectionViewSample/Utilities/MemoryLimitedCache.swift:50 |
| Combine scheduling | subscribe(on:) |
subscribe(on:) affects upstream subscription, request, and cancellation work. |
CollectionViewSample/Stores/AssetStore.swift:111 |
| Combine scheduling | receive(on:) |
receive(on:) selects the scheduler for downstream delivery. |
CollectionViewSample/Stores/AssetStore.swift:112 |
| Source import | UIKit |
The cited file imports this module; runtime use and architectural role are not inferred. | CollectionViewSample/AppDelegate.swift:8 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | CollectionViewSample/Stores/AssetStore.swift:8 |
| Source import | Combine |
The cited file imports this module; runtime use and architectural role are not inferred. | CollectionViewSample/PostGridViewController.swift:9 |
| Source import | UniformTypeIdentifiers |
The cited file imports this module; runtime use and architectural role are not inferred. | CollectionViewSample/Stores/AssetStore.swift:11 |
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
CollectionViewSample/Stores/ModelStore.swift:10 — representative type boundary
protocol ModelStore {
associatedtype Model: Identifiable
func fetchByID(_ id: Model.ID) -> Model
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
AppDelegate |
Receives callback-driven events | UIResponder, UIApplicationDelegate |
ModelStore |
Defines a capability or collaboration contract | Concrete collaborators/imported frameworks |
AssetStore |
Centralized state or persistence access | ModelStore |
AnyModelStore |
Centralized state or persistence access | Concrete collaborators/imported frameworks |
PostGridViewController |
View lifecycle, callbacks, and feature coordination | UIViewController |
SceneDelegate |
Receives callback-driven events | UIResponder, UIWindowSceneDelegate |
PlaceholderStore |
Centralized state or persistence access | FileBasedCache |
DestinationPostPropertiesView |
User-interface presentation and input forwarding | UIView |
SectionBackgroundDecorationView |
User-interface presentation and input forwarding | UICollectionReusableView |
Cache |
Defines a capability or collaboration contract | Concrete collaborators/imported frameworks |
The source explicitly defines local protocol relationships: AssetStore → ModelStore, FileBasedCache → Cache, MemoryLimitedCache → Cache.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
prefetchingIndexPathOperations (CollectionViewSample/PostGridViewController.swift:20) |
fileprivate |
Use is restricted to this source file. | Inference: share with same-file helpers or extensions without exposing the symbol module-wide. |
sectionHeaderElementKind (CollectionViewSample/PostGridViewController.swift:21) |
fileprivate |
Use is restricted to this source file. | Inference: share with same-file helpers or extensions without exposing the symbol module-wide. |
configureHierarchy (CollectionViewSample/PostGridViewController.swift:45) |
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. |
configureDataSource (CollectionViewSample/PostGridViewController.swift:53) |
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
CollectionViewSample/PostGridViewController.swift:20 — representative boundary
class PostGridViewController: UIViewController {
// ...
fileprivate var prefetchingIndexPathOperations = [IndexPath: AnyCancellable]()
// ...
}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 | PostGridViewController |
The source’s Controller suffix makes this role explicit. |
| Receives callback-driven events | AppDelegate, SceneDelegate |
The source’s Delegate suffix makes this role explicit. |
| Centralized state or persistence access | AnyModelStore, AssetStore, ModelStore, PlaceholderStore |
The source’s Store suffix makes this role explicit. |
| User-interface presentation and input forwarding | DestinationPostPropertiesView, SectionBackgroundDecorationView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | CollectionViewSample/PostGridViewController.swift:11 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Protocol-oriented abstraction | CollectionViewSample/Stores/AssetStore.swift:38 |
A local protocol and concrete conformance create an explicit capability boundary. |
| Delegate or data-source callbacks | CollectionViewSample/AppDelegate.swift:11 |
Callback protocols invert event delivery back into the sample’s owner. |
| Central store | CollectionViewSample/Stores/ModelStore.swift:16 |
A store-named type centralizes feature state or persistence. |
Naming conventions
- Types: Controller: PostGridViewController; Delegate: AppDelegate, SceneDelegate; Store: AnyModelStore, AssetStore, ModelStore, PlaceholderStore; View: DestinationPostPropertiesView, SectionBackgroundDecorationView.
- Protocols:
ModelStore,Cache. - Methods:
application,flatMapIfNil,withOutput,fetchByID,loadAssetByID,prepareAssetIfNeeded,prepareAsset,makeDownloadRequest. - Files:
CollectionViewSample/AppDelegate.swift,CollectionViewSample/Stores/AssetStore.swift,CollectionViewSample/Stores/ModelStore.swift,CollectionViewSample/DestinationPost.swift,CollectionViewSample/PostGridViewController.swift,CollectionViewSample/SceneDelegate.swift.
Architecture takeaways
AppDelegateis the main source-visible entry or composition anchor for this sample.- Framework work reaches UIKit, UniformTypeIdentifiers, CoreGraphics, ImageIO 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 |
|---|---|
CollectionViewSample/AppDelegate.swift |
Cited implementation, UIKit, AppDelegate |
CollectionViewSample/Stores/AssetStore.swift |
Cited implementation, DispatchQueue(label:), DispatchQueue.main, subscribe(on:), receive(on:), Foundation, UniformTypeIdentifiers, AssetStore, AssetError |
CollectionViewSample/Stores/ModelStore.swift |
ModelStore, AnyModelStore |
CollectionViewSample/PostGridViewController.swift |
Cited implementation, PostGridViewController, AnyCancellable, Combine |
CollectionViewSample/Utilities/UnfairLock.swift |
os_unfair_lock, UnfairLock, LockAssertion |
CollectionViewSample/Utilities/MemoryLimitedCache.swift |
NotificationCenter, Cache, MemoryLimitedCache |
CollectionViewSample/DestinationPost.swift |
Section, Identifier, Asset, DestinationPost |
CollectionViewSample/SceneDelegate.swift |
SceneDelegate |
CollectionViewSample/Utilities/PlaceholderStore.swift |
PlaceholderStore |
CollectionViewSample/Views/DestinationPostPropertiesView.swift |
DestinationPostPropertiesView |
CollectionViewSample/Views/SectionBackgroundView.swift |
SectionBackgroundDecorationView |
CollectionViewSample/Utilities/URLSession+DownloadTaskPublisher.swift |
DownloadTaskPublisher, DownloadTaskSubscription |
CollectionViewSample/Stores/SampleData.swift |
SampleData |
CollectionViewSample/Utilities/Appearance.swift |
Appearance |
CollectionViewSample/Utilities/FileBasedCache.swift |
FileBasedCache |
CollectionViewSample/Views/DestinationPostCell.swift |
DestinationPostCell |