2016-04-27 17 views
0

這裏是我的3個表如何從NSPredicate中獲取連接表中的所有元素? (對於NSFetchedResultsController)

House 
-userPermissions: [Permission] 

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

User 
-housePermissions: [Permission] 

我希望得到所有用戶(不考慮權限級別的),對於一個給定的房子。

我想使用NSFetchedResultsController(因爲我可能有10k用戶),這意味着我將不得不使用NSPredicate

我已經試過以下斷言:

NSPredicate(format: "SELF IN %@[email protected]", 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: "%@[email protected]", house.userAccesses ?? [])

崩潰:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "%@[email protected]"'

回答

2

如果希望用戶爲你的結果,你的抓取請求將在用戶實體上:

let req = NSFetchRequest(entityName: "User") 

然後,你的謂詞可以表示爲誰對給定家裏的任何housePermissions任何用戶:

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