KORData允许您创建一个项目,初始化Core Data并简化NSFetchedResultsController的使用。结果是您可以在几行代码内创建NSFetchedResultsController,带有或没有子类化。
在下面的示例中,AnimalController是一个具有嵌入式KORDataTableViewController的ViewController,KORDataTableViewController是由框架提供的。在prepare for segue中进行配置,无需子类化。以下显示了包含嵌入式KORDataTableViewController的整个ViewController。
@implementation AnimalController
// lightweight dependency injection methodology
- (void)postInitSetup:(ConfiguratorInjector *)inj farm:(Farm *)farm {
_animalFarmService = inj.animalFarmService;
_farm = farm;
}
// An embeded KORDataTableViewController is in the storyboard. Just inject a little behavior
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.destinationViewController isKindOfClass:[KORDataTableViewController class]]) {
KORDataTableViewController *kor = segue.destinationViewController;
[kor postInitSetupWithContext:self.animalFarmService.managedObjectContext
selectRowBlock:nil
cellIdentifier:@"AnimalFarmCell"
cellForRowBlock:^(Animal *animal, UITableViewCell *cell, NSIndexPath *indexPath) {
cell.textLabel.text = animal.name;
}];
kor.fetchRequest = [[self.animalFarmService animalsForFarm:self.farm] fetchRequest];
}
}
@end
注意:此示例使用服务类而不是“Animal”managed object子类上的分类。这是故意不是active record模式,这不是简化core data的唯一方法。示例还使用了animalFarmService中的KEYShadow类型安全查询框架。虽然这简化了代码,但它不是必需的。
pod 'KORData'
包含一个示例项目,KORDataExampleIOS。