NoahKamara / CompoundPredicate

A library for combining swift predicates
https://noahkamara.github.io/CompoundPredicate/documentation/compoundpredicate/
MIT License
49 stars 3 forks source link

FAQ: Handling Optional Values in SwiftData #1

Closed skalber-mm closed 8 months ago

skalber-mm commented 8 months ago

I'm getting a crash with the following error: image

It looks like the issue is in the currentRoomPredicate, if I remove it from the conjunction array it doesn't crash: image

Is there a limit on the allowed nested objects in the predicate? I mean is the problem with contract.listing?.room?.persistentModelID ?

Thank you for the great work making this lib!

NoahKamara commented 8 months ago

Thank you for the report :) It might be an issue with PersistentIdentifier or NilLiteral.

Can you provide a minimal reproducible example?

NoahKamara commented 8 months ago

I am trying to reproduce the issue and am getting a SwiftData crash when trying to access nested relationships:

@Model
class PersonModel {
    @Relationship
    var partner: PersonModel?
}

let nestedRelationshipPredicate = #Predicate<PersonModel> { person in
    person.partner?.partner?.persistentModelID == modelID
}

try context.fetch(.init(predicate: nestedRelationshipPredicate))

This seems to be an issue with SwiftData. I suggest trying to try unwrapping the optionals:

if let listing = contract.listing { ... } else { false }

I would welcome a minimal reproducible example as I am so far unable to reproduce this specific crash

skalber-mm commented 8 months ago

Yes thanks, managed to solve it by reading this article: https://fatbobman.com/en/posts/how-to-handle-optional-values-in-swiftdata-predicates/

image

NoahKamara commented 8 months ago

Happy to hear you could solve the issue. I'm closing it but marking it with FAQ for others with the same problem