【发布时间】:2014-05-18 09:46:03
【问题描述】:
我正在使用 TableViews 和 Core Data 编写一个简单的应用程序,它显示第一级的学生列表,当单击单元格时,它会显示学生所修课程的名称。我将student->courses设置为toMany,将courses->student设置为toOne。这就是我将 Student 实体传递给课程视图控制器以使用其 managedObjectContext 的方式:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Student *info = [_fetchedResultsController objectAtIndexPath:indexPath];
CoursesTableViewController *courseViewController = [[CoursesTableViewController alloc] initWithStudentInfo:info];
[self.navigationController pushViewController:courseViewController animated:YES];
}
这就是我在课程视图控制器中创建课程的方式(我只想要三个如下命名的课程,numberofrow 应该保证这一点):
- (void)viewDidLoad
{
[super viewDidLoad];
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][0];
int numberofrow = [sectionInfo numberOfObjects];
NSLog(@"%d",numberofrow);
NSLog(@"%d",[self.fetchedResultsController sections].count);
if(numberofrow <=2){
NSLog(@"creating courses");
[self insertNewObject:@"comp319"];
[self insertNewObject:@"comp314"];
[self insertNewObject:@"comp316"];
}
}
- (void)insertNewObject:(NSString *) str{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[newManagedObject setValue:str forKey:@"course_name"];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
我如何在课程视图控制器中创建 fetchedResultsController:
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Courses" inManagedObjectContext:self.studentInfo.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"course_name" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.studentInfo.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
最后的问题是,当我创建第一个学生并单击它时,上面的课程视图控制器的 viewDidLoad 中的 NSLog 会打印“创建课程”,但是当我创建第二个学生时它不会,并且 numberofrow 也会在它的位置打印 3应该打印 0,因为它是一个新的学生实例。
感谢您的帮助。
【问题讨论】:
-
在
CoursesTableViewController中创建fetchedResultsController时的NSPredicate是什么? -
我复制了上面的全部内容。