Skip to content

Housekeep/main actor data cache#54

Merged
jmarek41 merged 7 commits intomainfrom
housekeep/main-actor-data-cache
Mar 3, 2026
Merged

Housekeep/main actor data cache#54
jmarek41 merged 7 commits intomainfrom
housekeep/main-actor-data-cache

Conversation

@jmarek41
Copy link
Copy Markdown
Member

@jmarek41 jmarek41 commented Feb 25, 2026

Data cache on main actor -> prevent actor hops when reading/writing the changes from main actor

@samoilyk @bezoadam 👇 (part of Documentation.md)

Observation granularity: @Observable tracks at the stored property level. DataCache has one stored property — value. All computed properties that read dataCache.value (regardless of which sub-property) re-evaluate when any part of the model changes. For SwiftUI views this is fine — body re-evaluation is cheap and SwiftUI diffs the output. For imperative subscriptions, add manual deduplication or use Observations + .removeDuplicates() on iOS 26+.

@jmarek41 jmarek41 changed the base branch from main to housekeep/swift-6-migration February 25, 2026 16:21
Refactor observation tracking to use checked continuation for better control of async flow.
bezoadam
bezoadam previously approved these changes Feb 26, 2026
samoilyk
samoilyk previously approved these changes Feb 26, 2026
eRDe33
eRDe33 previously approved these changes Feb 27, 2026
@ssestak
Copy link
Copy Markdown
Contributor

ssestak commented Mar 2, 2026

Template encourages one-shot assignment

The ExampleComponentModel template only shows a comment inside onAppear, which nudges developers toward:

func onAppear() async {                                                                                                                                                                                            
   items = dataCache.value.items                                                                                                                                                                                  }

This is a one-time copy. If a modal adds/modifies an item in the cache, the original screen won't reflect the change.

Computed properties should be the primary pattern

var items: [Item] { dataCache.value.items }

@Observable tracks reads through the entire chain — SwiftUI views reading this computed property automatically re-evaluate when dataCache.value changes. No manual subscription needed.

Suggestion: The template and Documentation.md should show computed properties as the primary usage pattern, with onAppear reserved for one-time setup/fetching.

Documentation hierarchy for observation patterns

The documentation covers three observation approaches but could benefit from a clearer hierarchy with concrete use cases:

  1. Computed properties (display) — primary pattern for 95%+ of cases. Currently missing from the template and docs as an explicit best practice example.
  2. withObservationTracking (imperative side effects) — correctly documented, but would benefit from a concrete use case, e.g. triggering a network re-fetch when a related cache value changes, firing an analytics
    event on state transition, or performing navigation based on cache state.
  3. Observations + removeDuplicates() (iOS 26+) — good to have for completeness as a future replacement for the manual withObservationTracking loop.

Populate breaking change

T.Element now requires Identifiable (was just Equatable), and merging is now ID-based (update in-place + append). Better behavior, but this is a breaking change worth calling out in the PR description.

@jmarek41
Copy link
Copy Markdown
Member Author

jmarek41 commented Mar 3, 2026

@ssestak I updated the template and docs. I'll also mention breaking changes in release notes.

Base automatically changed from housekeep/swift-6-migration to main March 3, 2026 15:01
@jmarek41 jmarek41 dismissed stale reviews from eRDe33, samoilyk, and bezoadam March 3, 2026 15:01

The base branch was changed.

@jmarek41 jmarek41 enabled auto-merge March 3, 2026 15:03
@jmarek41 jmarek41 disabled auto-merge March 3, 2026 15:08
@jmarek41 jmarek41 merged commit 646f9c8 into main Mar 3, 2026
@jmarek41 jmarek41 deleted the housekeep/main-actor-data-cache branch March 3, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants