All skills
dpearson2699 avatar

/swiftdata

@cf3fe87

Implement, review, or improve data persistence using SwiftData. Use when defining @Model classes with @Attribute, @Relationship, @Transient, #Unique, or #Index; when querying with @Query, #Predicate, FetchDescriptor, or SortDescriptor; when configuring ModelContainer and ModelContext for SwiftUI or background work with @ModelActor; when planning schema migrations with VersionedSchema and SchemaMigrationPlan; when setting up CloudKit sync with ModelConfiguration; or when coexisting with or migrating from Core Data.

Use this Skill: https://skilld.dev/gh/dpearson2699/swift-ios-skills/swiftdata

This session only. Nothing lands on disk.

referencespredicate-pitfalls.md

≈684 tokens on demand. Your agent reads this file only when SKILL.md points to it.

#Predicate Pitfalls

Common runtime crashes and build errors when using #Predicate with SwiftData.

Unsupported Expressions

#Predicate compiles Swift expressions into a query representation. Only a subset of Swift is supported. Unsupported expressions compile but crash at runtime.

Pattern Crash? Fix
$0.name.uppercased() == "PARIS" Runtime crash Use localizedStandardContains or caseInsensitiveCompare
$0.name.count > 5 Runtime crash Store length in a separate property or filter after fetch
$0.tags.count == 0 Runtime crash Use $0.tags.isEmpty (iOS 17.4+) or a stored flag
$0.name.isEmpty on optional String Runtime crash on some OS versions Use $0.name == nil || $0.name == ""
Custom computed property Runtime crash Only stored @Attribute properties work in predicates
Date.now captured by value Stale predicate Create a let now = Date() before the predicate and capture it
Enum raw value comparison Runtime crash (pre-iOS 18) Store the raw value as a separate property, or target iOS 18+
Loops, declarations, mutation, or switch-heavy control flow Build error or runtime failure Use boolean logic, ternary expressions, optional chaining, or optional binding patterns supported by Foundation Predicate
Arbitrary method calls Runtime crash Only supported methods (see below) work

Supported Operations

Comparisons: ==, !=, <, <=, >, >=

Logic: &&, ||, !

String: localizedStandardContains(_:), contains(_:), starts(with:), caseInsensitiveCompare(_:)

Collections: contains(where:), allSatisfy(_:), filter(_:), .isEmpty

Other: optional chaining, nil coalescing (??), ternary (? :), arithmetic (+, -, *, /), type casting (as?, is)

Safe Pattern: Build Predicates Dynamically

func tripPredicate(searchText: String, favoritesOnly: Bool) -> Predicate<Trip> {
    let now = Date()
    return #Predicate<Trip> { trip in
        (searchText.isEmpty || trip.destination.localizedStandardContains(searchText))
        && (!favoritesOnly || trip.isFavorite)
        && trip.startDate > now
    }
}

Capture all external values as let bindings outside the predicate closure. The predicate captures them by value at creation time.

Debugging Predicate Crashes

When a predicate crashes at runtime with SwiftData.PredicateError or NSInvalidArgumentException:

  1. Simplify the predicate to a single clause and add clauses back one at a time
  2. Check each clause uses only supported operations on stored properties
  3. Test on the minimum deployment target — some operations were added in later iOS versions

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The skill provides valid reference documentation and code snippets for working with Apple's SwiftData persistence framework. No malicious actions or security risks were identified.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    1/4 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at cf3fe87. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Steadyupdated 3 months ago

README badge

README badge for dpearson2699/swift-ios-skills/swiftdata