“setPropertiesToFetch中的许多关系无效”错误尝试将多对多关系分组

时间:2012-04-28 20:09:59

标签: ios uitableview core-data many-to-many nsfetchedresultscontroller

我是CoreData的新手,我正在为未来的项目做一些调整。

我正在尝试显示由他/她的歌手分组的歌曲列表。一首歌可以由很多歌手演唱,一首歌手可以演唱很多歌曲:-)这就是我的模特:

enter image description here

然后我有很多关系。

当我尝试将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

谢谢

0 个答案:

没有答案