具有多对多关系的核心数据

时间:2011-10-17 12:21:50

标签: iphone core-data

您好我正在保存歌曲实体的歌词段数。现在我想更新那个歌词

我使用下面的代码来更新值。但它正在创建新的记录..并告诉删除

- (void)editLyrics {

     [editBarbutton setTitle:@"Save"];



    lyrics  = [NSEntityDescription insertNewObjectForEntityForName:@"Lyrics" inManagedObjectContext:managedObjectContext];
    lyrics.songLyrics = lyricsTextview.text;





    lyrics.startTime = startTimeText.text;






    lyrics.endTime = endTimeText.text;

     lyrics.lyricsSong = song;    
    NSError *error;
    // here's where the actual save happens, and if it doesn't we print something out to the onsole
    if (![managedObjectContext save:&error])
    {
        NSLog(@"Problem saving: %@", [error localizedDescription]);
    } 
    }

1 个答案:

答案 0 :(得分:1)

您必须先获得当前对象:

NSError *error = nil;
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:[NSEntityDescription entityForName:@"Lyrics"
                               inManagedObjectContext:self.moc]];
[request setPredicate:[NSPredicate predicateWithFormat:@"something what u like to filter"]];
NSArray *lyrics = [self.moc executeFetchRequest:request error:&error];
if (error) NSLog(@"Failed to executeFetchRequest to data store: %@ in function:%@", [error localizedDescription],NSStringFromSelector(_cmd));
lyrics = [lyrics lastObject]