5

我是 CoreData 的新手,我正在为未来的项目使用它。

我正在尝试显示按他/她的歌手分组的歌曲列表。一首歌可以被很多歌手唱,一个歌手可以唱很多首歌:-) 那是我的模型:

在此处输入图像描述

然后我有一个多对多的关系。

当我尝试将 sectionNameKeyPath 设置为 songSingers.name 时,它​​会因错误而崩溃:

 'NSInvalidArgumentException', reason: 'Invalid to many relationship in setPropertiesToFetch: (songSingers.name)'

为什么我收到此错误?下面是一些代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self addDummyData];

    NSError *anyError = nil;
    BOOL success = [self.fetchedResultsController performFetch:&anyError];

    if( !success ) NSLog(@"Error = %@", anyError);
}

- (void)addDummyData
{
    [self deleteAllObjects:@"Song"];
    [self deleteAllObjects:@"Singer"];
    [self deleteAllObjects:@"Lyric"];

    // Lyrics
    Lyric *l = [NSEntityDescription insertNewObjectForEntityForName:@"Lyric" inManagedObjectContext:managedObjectContext]; 
    l.name = @"Lyric 1";

    // Songs
    Song *s = [NSEntityDescription insertNewObjectForEntityForName:@"Song" inManagedObjectContext:managedObjectContext];
    s.name = @"Song 1";

    Song *s2 = [NSEntityDescription insertNewObjectForEntityForName:@"Song" inManagedObjectContext:managedObjectContext];
    s2.name = @"Song 2";

    Song *s3 = [NSEntityDescription insertNewObjectForEntityForName:@"Song" inManagedObjectContext:managedObjectContext];
    s3.name = @"Song 3";

    // Singers
    Singer *sg = [NSEntityDescription insertNewObjectForEntityForName:@"Singer" inManagedObjectContext:managedObjectContext];
    sg.name = @"Singer 1";

    Singer *sg2 = [NSEntityDescription insertNewObjectForEntityForName:@"Singer" inManagedObjectContext:managedObjectContext];
    sg2.name = @"Singer 2";

    // Relations
    sg.singerSongs  = [NSSet setWithObjects:s, s2, nil];
    sg2.singerSongs = [NSSet setWithObjects:s2, s3, nil];

    s.songSingers   =  [NSSet setWithObjects:sg, nil];    
    s2.songSingers  =  [NSSet setWithObjects:sg, sg2, nil];
    s3.songSingers = [NSSet setWithObjects:sg2, nil];    

    s.songLyric = l;
    s2.songLyric = l;


    NSError *anyError = nil;
    BOOL success = [managedObjectContext save:&anyError];
    if(!success)
    {
        NSLog(@"Error: %@", anyError);
    }

}
- (NSFetchedResultsController *)fetchedResultsController {
    if (!fetchedResultsController) 
    {
        NSFetchRequest *request = [[NSFetchRequest alloc] init];

        [request setEntity:[NSEntityDescription entityForName:@"Song" 
                                       inManagedObjectContext:managedObjectContext]];

        NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" 
                                                                       ascending:YES];
        NSSortDescriptor *singerDescriptor = [[NSSortDescriptor alloc] initWithKey:@"songSingers.name" 
                                                                       ascending:YES];

                                                                         ascending:YES];        

        [request setSortDescriptors:[NSArray arrayWithObjects:nameDescriptor,nil]];

        NSString *sectionNameKeyPath = @"songSingers.name";

        NSFetchedResultsController *myFetchedResultsController
            = [[NSFetchedResultsController alloc] initWithFetchRequest:request 
                                              managedObjectContext:managedObjectContext
                                                sectionNameKeyPath:sectionNameKeyPath 
                                                         cacheName:nil];


        myFetchedResultsController.delegate = self;
        self.fetchedResultsController = myFetchedResultsController;
    }

    return fetchedResultsController;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Singer" 
                                              inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSError *anyError = nil;
    NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest 
                                                                  error:&anyError];
    Singer *sg = [fetchedObjects objectAtIndex:section];
    return sg.name; 

}

您可以看到我认为定义明确的虚拟数据“addDummyData”:^P

  • 歌曲 1 和 2 是给歌手 1 的
  • 歌曲 2 也适用于歌手 2
  • 歌曲 3 是给歌手 2 的

谢谢

4

0 回答 0