Post

Replies

Boosts

Views

Activity

Reply to SwiftData #Predicate cannot test for nil relationship
I did a bit more experimenting. The problem doesn't seem to be in the optionality of the types per se, as even the following doesn't work: @Model final class Item { var timestamp: Date var group: Group } @Model final class Group { var name: String var items: [Item] } What does work is comparing the objects' IDs: This works: func query(group: Group) { let id = group.id let predicate = #Predicate<Item> { item in item.group?.id == id // Compiles } } However, if the id is not in a local variable, the Predicate macro doesn't compile: func query(group: Group) { let predicate = #Predicate<Item> { item in item.group?.id == group.id // Doesn't compile } }
Sep ’23
Reply to SwiftData #Predicate cannot test for nil relationship
@newwbee Can you please show your code? I have just tested and neither of those variations (where you put the inverse) work: @Model final class Item { var timestamp: Date @Relationship var group: Group? } @Model final class Group { var name: String @Relationship(inverse: \Item.group) var items: [Item] } @Model final class Item { var timestamp: Date @Relationship(inverse: \Group.items) var group: Group? } @Model final class Group { var name: String @Relationship var items: [Item] }
Sep ’23
Reply to SwiftData #Predicate cannot test for nil relationship
Still an issue with the final release of iOS 17. Given the following models (initializers omitted for brevity): @Model final class Item { var timestamp: Date var group: Group? } @Model final class Group { var name: String var items: [Item] } This predicate doesn't compile: func query(group: Group) { let predicate = #Predicate<Item> { $0.group == group } } Force-unwrapping doesn't work either: $0.group! == group.
Sep ’23