IOS核心数据基数问题

时间:2014-05-18 09:46:03

标签: ios core-data

我正在使用TableViews和Core Data编写一个简单的应用程序,它显示了第一级学生的列表,当单击一个单元格时,它显示了学生所学课程的名称。我将student->课程设置为多个,课程 - >学生一个。这就是我如何将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();
    }
}

我如何在课程View Controller中创建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;
}

最后问题是,当我创建第一个学生并点击它时,NSLog in viewDidLoad of courses查看控制器上面的打印&#34;创建课程&#34;但是当我创建第二个学生时它不会也是numberofrow打印3应该打印0,因为它是一个新的学生实例。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

替换

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Courses" inManagedObjectContext:self.studentInfo.managedObjectContext];
[fetchRequest setEntity:entity];

// my comment : entity here has to be your student name, why 'course'
// and also replace your search key from 'yourCourseNameKey' with 'yourStudentNameKey'
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Your Student Name" inManagedObjectContext:self.studentInfo.managedObjectContext];
[fetchRequest setEntity:entity];

您需要获得学生人数,但您需要查询课程数量

答案 1 :(得分:0)

您没有为提取给提取的请求控制器的提取设置谓词,因此您不会搜索所选学生的课程,如果他们不在那里则创建。不管怎样这不是理想的(并且因为你没有在学生和所有课程之间建立关系而无法工作)。

所以,你很可能应该有一套课程和一组学生。关系应该很多:许多课程确实有多个学生,每个学生都需要多门课程(或者无论如何)。

现在,您的学生和课程将被预先创建和#39;然后你的视图控制器只处理获取,显示和连接(建立关系)对象。