我怎样才能从 NSPredicate 获取连接 table 中的所有元素? (对于 NSFetchedResultsController)

How can I get all elements in a join table from NSPredicate? (for NSFetchedResultsController)

这是我的 3 张桌子

House
-userPermissions: [Permission]

Permission
-house: House
-user: User
-permissionLevel: String

User
-housePermissions: [Permission]

我想获取给定房屋的所有用户(无论权限级别如何)。

我想使用 NSFetchedResultsController(因为我可能有 10k 用户)这意味着我将不得不使用 NSPredicate.

我试过以下谓词:

NSPredicate(format: "SELF IN %@.@user", house.userAccesses ?? [])

崩溃:Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFSet 0x128f3b070> valueForUndefinedKey:]: this class is not key value coding-compliant for the key user.'

NSPredicate(format: "%@.@user", house.userAccesses ?? [])

崩溃:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "%@.@user"'

如果您想要用户作为结果,您的提取请求将针对用户实体:

let req = NSFetchRequest(entityName: "User")

然后,您的谓词可以表示为对给定房屋具有任何 housePermissions 的任何用户:

req.predicate = NSPredicate(format: "ANY housePermissions.house = %@", h1)