如何设置一对多表格核心数据?

时间:2013-09-20 21:57:26

标签: ios core-data one-to-many

数据设置


项目

- 项目名称
-perHour

关系

-relatedTime(to MANY)


时报

-dateAdded
-totalTime

关系
-relatedProject(to ONE)


这是我目前的核心数据设置。我会解释我正在尝试做什么。我想这样做,以便“项目”存储在一个表格视图控制器中,当你点击一个特定的表格单元格时,它会带你到另一个表格视图控制器上显示所有“时间”。

目前这是我的Fetch的代码。

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Times" inManagedObjectContext:self.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:@"totalTime" 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.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"relatedProject == %@", self.detailItem];
    [fetchRequest setPredicate:predicate];


    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;
}

并且表的格式与masterViewController上的格式相同,因此没有理由不应该工作。但每当我点击“项目”的表格单元格时,它就会出现此错误:

2013-09-20 17:41:57.595 test3[2642:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Projects''
*** First throw call stack:
(0x2d605f53 0x379dc6af 0x2d35b32b 0x78bd5 0x785df 0x2fe4a2f5 0x2fe4a23d 0x2fe49fd9 0x2feae871 0x2febc647 0x2fd9139b 0x2ff07e17 0x2fe25a83 0x2fe2588d 0x2fe25825 0x2fd77023 0x2fa0024b 0x2f9fba5b 0x2f9fb8ed 0x2f9fb2ff 0x2f9fb10f 0x2f9f4e3d 0x2d5d11d5 0x2d5ceb79 0x2d5ceebb 0x2d539ce7 0x2d539acb 0x3225a283 0x2fddba41 0x799bd 0x37ee4ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

0 个答案:

没有答案