我想从表B中获得所有记录,其中B.ID = A.ID (连接A/B)。因此,我创建了一个传递对象A的方法,使用谓词:
-(NSArray*) getRecordFromB:(A*) a{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"a == %@", a];
//Return notes from pin
return [B MR_findAllWithPredicate:predicate1 inContext:localContext];
}而且,它工作得很完美。但是,我认为更好(我希望保留内存),所以我只将ObjectID传递给方法来进行连接。我改变了方法,因为:
-(NSArray*) getRecordFromB:(NSO*) a{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"a.objectID == %@", a];
//Return notes from pin
return [B MR_findAllWithPredicate:predicate1 inContext:localContext];
}但是没有起作用,因为我的模型A没有这个属性。有什么更好的方法来连接两个表吗?还是我用我的第一种方法?
发布于 2013-11-23 21:48:01
您可以在fetch请求中传递一个对象id,而不是托管对象:
[NSPredicate predicateWithFormat:@"a == %@", a.objectID];就像
[NSPredicate predicateWithFormat:@"a == %@", a];如果您已经有了"A“对象,那么如果您将a或a.objectID传递给fetch请求,则没有任何区别。
https://stackoverflow.com/questions/20168168
复制相似问题