我厌倦了不断地为我的项目放置/重复NSFetchedResultsController代码,几乎在每个使用Managed Object Context的文件中。我可以把NSFetchedResultsController放在托管对象类中吗?
我想减少重复代码的数量。我想在模型类中放置这种类似于重复的类似CRUD的代码。
我想取而代之的是把我所有的自定义NSFetchs的管理对象类内的相关实体(即:Company.m,Employee.m)。
即使从苹果的核心书籍示例代码不把这段代码到管理对象类,如果它的可能,我想知道?
我试过的代码粘贴到我的Company.m类,但它一直在抱怨managedObjectContext并且还抱怨说,fetchedResultsController未声明,即使它的参数?
理想情况下,我想提出许多不同类型的读取请求/结果控制器东西实体管理对象类里面太的。
但在此之前我让自己的未来,是否有可能,因此,只是把所有的东西NSFetchedResultsController实体管理对象类里面?
如果有一个示例教程或项目或源代码涵盖了这一点,那也会很棒。
谢谢。
(代码示例如下)。
/**
Returns the fetched results controller. Creates and configures the controller if necessary.
*/
- (NSFetchedResultsController *)fetchedResultsController
{
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
// Create and configure a fetch request with the Book entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Create the sort descriptors array.
NSSortDescriptor *authorDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:authorDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller.
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"author" cacheName:@"Root"];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;
// Memory management.
[aFetchedResultsController release];
[fetchRequest release];
[authorDescriptor release];
[sortDescriptors release];
return fetchedResultsController;
}
好的,谢谢。 – zardon 2011-02-08 03:46:26