FAQ: Handling Optional Values in SwiftData #1

Closed
opened 2024-03-17 09:56:53 +00:00 by skalber-mm · 4 comments
skalber-mm commented 2024-03-17 09:56:53 +00:00 (Migrated from github.com)

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!

I'm getting a crash with the following error: ![image](https://github.com/NoahKamara/CompoundPredicate/assets/14924551/77126aa4-660a-4159-86d1-7e1c3c7fb7c0) It looks like the issue is in the `currentRoomPredicate`, if I remove it from the `conjunction` array it doesn't crash: ![image](https://github.com/NoahKamara/CompoundPredicate/assets/14924551/f05acd37-618f-4d9e-9d6e-9611faf1efa2) 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 2024-03-20 20:15:38 +00:00 (Migrated from github.com)

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

Can you provide a minimal reproducible example?

Thank you for the report :) It might be an issue with `PersistentIdentifier` or `NilLiteral`. Can you provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)?
NoahKamara commented 2024-03-20 20:35:51 +00:00 (Migrated from github.com)

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

I am trying to reproduce the issue and am getting a SwiftData crash when trying to access nested relationships: ```swift @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: ```swift 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 2024-03-22 18:48:46 +00:00 (Migrated from github.com)

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

image

Yes thanks, managed to solve it by reading this article: https://fatbobman.com/en/posts/how-to-handle-optional-values-in-swiftdata-predicates/ ![image](https://github.com/NoahKamara/CompoundPredicate/assets/14924551/aba9ec17-1162-441f-bee1-257e105ead9b)
NoahKamara commented 2024-03-23 12:59:43 +00:00 (Migrated from github.com)

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

Happy to hear you could solve the issue. I'm closing it but marking it with FAQ for others with the same problem
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
noah/CompoundPredicate#1
No description provided.