我想你向后接近的问题。如果您有关键字,则应该搜索Keyword
对象,然后走他们的A
关系以查找相关的A
对象。
然后检查关系集中的重叠。
因此,像:
NSFetchRequest *fetch=//...set up fetch
[fetch setEntity:Keyword_entity];
NSPredicate *p=[NSPredicate predicateWithFormat:@"keyword IN %@",keywords];
[fetch setPredicate:p];
NSArray *fetchedObj=//... execute fetch
NSMutableSet *inCommon=[NSMutableSet setWithCapacity:[fetchedObjs count]];
for (NSManagedObject *mo in fetchedObjs){
if ([inCommon count]==0){
[inCommon addObjects:[mo valueForKey:@"aRelationshipName"]];
}else{
[inCommon intersectSet:[mo valueForKey:@"aRelationshipName"]];
}
}
...
inCommon
随后将含有一组共享所有关键字的所有独特
A
对象。
更新:
从OP作者:
这并不完全解决问题 作为来自fetchedObjs的 路口回的结果是不正确的 。它并不保证所有 'A'对象包含提供的所有关键字 。
好的,让我们再试一次。假设你有一个看起来像这样的数据模型:
A {
keywords<<-->>Keyword.as
}
Keyword{
keyword:string
as<<-->>A.keywords
}
那么这样的事情应该工作:
NSFetchRequest *keyWordFetch=//...set up fetch
[keyWordFetch setEntity:Keyword_entity];
NSPredicate *p=[NSPredicate predicateWithFormat:@"keyword IN %@",keywords];
[keyWordFetch setPredicate:p];
NSArray *fetchedKeywords=//... execute keyWordFetch
NSFetchRequest *entityAFetch=//...set up fetch
[entityAFetch setEntity:A_entity];
NSPredicate *pred=[NSPredicate predicateWithFormat:@"ALL keywords IN %@", fetchedKeywords);
[entityAFetch setPredicate:pred];
NSArray *fetchedAs=//... execute fetch
如果您只是查询给定A的关键字,那么性能如何?我的意思是,你可以重写谓词来做子查询作为主查询。 – 2011-04-22 00:20:03