CoreData:subTable上的fetchRequest

时间:2013-09-05 15:11:39

标签: ios core-data

enter image description here

如何向Subcategorie添加Categorie? 我怎样才能获得Subcategorie的具体Categorie

我需要像

这样的东西
  

获取Subcategorie.denumire,其中Categorie.denumire ==“somename”

add mySubcategorie to Categorie.denumire where Categorie.denumire == "somename"

我该怎么做?如何获取子表的父表的名称和表的子表的名称?

2 个答案:

答案 0 :(得分:0)

当您生成NSManagedObject实体时,Goal类将有一个名为toMinorGoal的NSSet(假设您的toMinorGoal是无序关系)。此外,XCode将生成4种附件方法,用于向/从关系中添加/删除MinorGoal对象。

如果你需要获取MinorGoals对象,你只需要获取Goal对象,然后访问其包含所有MinorGoal对象的toMinorGoals NSSet。或者,你可以只获取MinorGoal对象,但是这些对象将返回它们中的每一个(如果你没有指定你想要的数量)。

这是XCode为您提供的生成访问器的近似示例:

- (void)addtoMinorGoaObject:(MinorGoal *)value;
- (void)removetoMinorGoalObject:(MinorGoal *)value;
- (void)addtoMinorGoal:(NSSet *)value;
- (void)removetoMinorGoal:(NSSet *)value;

希望它对你有所帮助。

答案 1 :(得分:0)

经过几天尝试不同的解决方案后,我终于想出了这个,感谢CoreData上的这个教程: http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started

我提取了表Subcategorie的所有子表Categorie,如下所示:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Subcategorie"
                                          inManagedObjectContext:self.managedObjectContext];
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"categorii.denumire == %@",self.title];
[fetchRequest setPredicate:predicate];
[fetchRequest setEntity:entity];


NSError *error;
self.listaElementeBD = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

self.title是denumire的{​​{1}},希望这有帮助。