1

Every time I run the predicate I get an error
xCode version 15.3
Destination macOS Sonoma, 14.4
Swift 5

@AppStorage(UserDefaults.Constants.selectedFilterName) static var selectedFilterName: String?
@Query(filter: #Predicate<Filter> {
    $0.name == (selectedFilterName ?? "")
}) var selectedFilters: [Filter]

Model:

@Model
class Filter: Hashable, Identifiable {
    var id: String { name }
    let name: String
}

Stack:

can't use NULL on left hand side
(
    0   CoreFoundation                      0x000000019c17eccc __exceptionPreprocess + 176
    1   libobjc.A.dylib                     0x000000019bc66788 objc_exception_throw + 60
    2   CoreData                            0x00000001a29cb744 -[NSSQLGenerator newSQLStatementForRequest:ignoreInheritance:countOnly:nestingLevel:nestIsWhereScoped:requestContext:] + 4040
    3   CoreData                            0x00000001a29ca62c -[NSSQLiteAdapter newSelectStatementWithFetchRequestContext:ignoreInheritance:] + 140
    4   CoreData                            0x00000001a29ca564 -[NSSQLFetchRequestContext _createStatement] + 52
    5   CoreData                            0x00000001a29ca34c -[NSSQLFetchRequestContext fetchStatement] + 172
    6   CoreData                            0x00000001a29ca22c -[NSSQLFetchRequestContext executeRequestCore:] + 28
    7   CoreData                            0x00000001a29c9e94 -[NSSQLStoreRequestContext executeRequestUsingConnection:] + 300
    8   CoreData                            0x00000001a29c9bbc __52-[NSSQLDefaultConnectionManager handleStoreRequest:]_block_invoke + 60
7
  • 1
    Is Filter.name optional? Commented Mar 16, 2024 at 13:21
  • no, let name: String Commented Mar 16, 2024 at 13:59
  • 1
    when your predicate depends on a property wrapped value usually you need to create use a false predicate and then set the real predicate that uses the value at top of body. That was the case for @FetchRequest I'm not certain for @Query though. Commented Mar 16, 2024 at 14:03
  • FetchRequest uses NSPredicates, that ask Filter to be inherited from NSObject and being NSManagedObject. I'd like to avoid this as I don't really understand how it would work with @Model :[ I'll try to set the real predicate in the body anyway but now without FetchRequest if it possible Commented Mar 16, 2024 at 14:13
  • 1
    You misunderstood what mahal meant, he was only comparing to FetchRequest not saying you should use it. You can not use anything related to Core Data when using SwiftData. I believe he meant you should do something similar to what I did in the last part of this answer Commented Mar 16, 2024 at 14:20

1 Answer 1

0

@JoakimDanielson and @malhal - thanks!
Answer is: @Query(filter: Predicate<Filter>.false) var selectedFilters: [Filter]

init() {
    selectedFilterName = ""
    var fetchDescriptor = FetchDescriptor<Filter>(predicate: #Predicate { $0.name == (selectedFilterName ?? "") })
    fetchDescriptor.fetchLimit = 1
    _filters = Query(fetchDescriptor)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.