Hi,
Yes I get the same results on a new project. I have created an example project and attached it here. It's based on a basic Core Data app, with an "Item" entity, that has a one-to-many relationship with a "Tag" entity. When I convert these into "App Entities", I can create a query for the properties of the "Item" entity, and another query for the "Tags" entity, but I can't figure out how to create a query in "Item" that would also look for associated 'tags'.
I can't seem to attach a zip file containing the app project to this post for some reason. But the code is fairly simple.
This is the Item AppEntity:
import Foundation
import AppIntents
struct ItemsAppEntity: AppEntity {
static var defaultQuery = ItemsAppEntityQuery()
var id: String
@Property(title: "Timestamp")
var timestamp: Date
@Property(title: "Tags")
var tags: [CJTagItemsAppEntity]
static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "Test Item")
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(timestamp.formatted())")
}
struct ItemsAppEntityQuery: EntityPropertyQuery {
static var sortingOptions = SortingOptions {
SortableBy(\ItemsAppEntity.$timestamp)
}
typealias ComparatorMappingType = NSPredicate
static var properties = QueryProperties {
Property(\ItemsAppEntity.$timestamp) {
LessThanComparator { NSPredicate(format: "timestamp < %@", $0 as NSDate) }
GreaterThanComparator { NSPredicate(format: "timestamp > %@", $0 as NSDate) }
}
// HOW TO ADD SEARCH-BY-TAGNAME ... this does't work
Property(\CJTagItemsAppEntity.$tagName, entityProvider: { item in
return item.tags.first!
}) {
EqualToComparator {name in
let predicateFormat = "tagName == '\(name)'"
return NSPredicate(format: predicateFormat)
}
}
}
func entities(for identifiers: [ItemsAppEntity.ID]) async throws -> [ItemsAppEntity] {
return []
}
func entities(matching comparators: [NSPredicate], mode: ComparatorMode, sortedBy: [EntityQuerySort<ItemsAppEntity>], limit: Int?) async throws -> [ItemsAppEntity] {
return []
}
}
}
This is the Tags AppEnttiy:
struct CJTagItemsAppEntity: AppEntity {
static var defaultQuery = CJTagItemsAppEntityQuery()
var id: String
@Property(title: "Name")
var tagName: String
static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "Test Tag")
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(tagName)")
}
struct CJTagItemsAppEntityQuery: EntityPropertyQuery {
static var sortingOptions = SortingOptions {
SortableBy(\CJTagItemsAppEntity.$tagName)
}
typealias ComparatorMappingType = NSPredicate
static var properties = QueryProperties {
Property(\CJTagItemsAppEntity.$tagName) {
EqualToComparator {name in
let predicateFormat = "tagName == '\(name)'"
return NSPredicate(format: predicateFormat)
}
}
}
func entities(for identifiers: [CJTagItemsAppEntity.ID]) async throws -> [CJTagItemsAppEntity] {
return []
}
func entities(matching comparators: [NSPredicate], mode: ComparatorMode, sortedBy: [EntityQuerySort<CJTagItemsAppEntity>], limit: Int?) async throws -> [CJTagItemsAppEntity] {
return []
}
}
}
I haven't implemented any methods properly ... I'm just trying to get the syntax right for this.
Thanks.