在循环中编辑核心数据模型对象

时间:2017-02-02 11:54:42

标签: ios objective-c xcode core-data

我正在尝试编辑Core Data存储中的现有对象。当我在没有循环的情况下编辑每个对象的属性时,这可以正常工作,但是当我尝试在for循环中执行它时,只有我的最后一个对象获得新属性。 这是我用来编辑没有循环的对象的代码。这段代码工作正常):

NSFetchRequest *songsRefreshRequest = [[NSFetchRequest alloc] initWithEntityName:@"Music"];
songsRefreshRequest.predicate = nil;
songsRefreshRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name"
                                                                      ascending:YES
                                                                       selector:@selector(localizedStandardCompare:)]];
NSError *frError;
NSArray *fetchedSongs = [self.context executeFetchRequest:songsRefreshRequest error:&frError];
fetchedSongs ? : NSLog(@"Error in fetch while refreshing songs: %@", [frError localizedDescription]);


NSString *song1path = [[NSBundle mainBundle] pathForResource:@"Beautiful-birds-song-in-the-morning" ofType:@"mp3"];
NSString *song2path = [[NSBundle mainBundle] pathForResource:@"Birds-singing-relaxation" ofType:@"mp3"];
NSString *song3path = [[NSBundle mainBundle] pathForResource:@"Morning Melody" ofType:@"mp3"];

Music *song1 = (Music *) [fetchedSongs objectAtIndex:0];
Music *song2 = (Music *) [fetchedSongs objectAtIndex:1];
Music *song3 = (Music *) [fetchedSongs objectAtIndex:2];

song1.name = @"Beautiful-birds-song-in-the-morning";
song2.name = @"Birds-singing-relaxation";
song3.name = @"Morning Melody";

song1.path = song1path;
song2.path = song2path;
song3.path = song3path;

这是我用来编辑循环中歌曲属性的代码(下面的代码以某种方式编辑仅最后一个对象的路径(在我的情况下为song3),其他对象的path属性变为NULL。我无法弄明白为什么):

for (Music *iterSong in fetchedSongs){
    NSString *iterSongName = [iterSong valueForKey:@"name"];
    iterSong.path = [[NSBundle mainBundle] pathForResource: iterSongName ofType:@"mp3"];
}

我已尝试将for each更改为for (int i=0; i<[fetchedSongs count]; i++)这没有用。循环重复的次数与模型中的许多歌曲一样多,只会改变最后一首歌曲的路径。感谢任何帮助,提前谢谢!

P.S。整个循环代码:

for (Music *iterSong in fetchedSongs){
        //Music *iterSong = (Music*) [fetchedSongs objectAtIndex:i];
        NSLog(@"inerSong.name = %@", iterSong.name);
        NSLog(@"inerSong.oldPath = %@", iterSong.path);

        NSString *iterSongName = [iterSong valueForKey:@"name"];
        NSString *iterSongPath = [[NSBundle mainBundle] pathForResource: iterSongName ofType:@"mp3"];
        NSLog(@"iterSongPath (string) = %@", iterSongPath);
        iterSong.path = iterSongPath;
        NSLog(@"iterSong.newPath = %@", iterSong.path);

    }

    if ([self.context hasChanges]){
        NSError *error;
        if (![self.context save:&error]){
            NSLog(@"error while saving context after refreshing song paths: %@", [error localizedDescription]);
        }
    }

2 个答案:

答案 0 :(得分:0)

首先,我假设您在NSManagedObjectContext performBlock方法中运行该循环。其次,在编辑对象后,需要save your changes到NSManagedObjects上下文。

<强>目标-C:

NSError *error = nil;
if ([[self managedObjectContext] save:&error] == NO) {
    NSAssert(NO, @"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]);
}

<强>夫特:

do {

        try managedObjectContext.save()
    } catch {
        fatalError("Failure to save context: \(error)")
    }

答案 1 :(得分:0)

问题解决了这个问题: 我从文件名中删除了所有连字符。并且代码开始在相同的循环中正常工作。我还将文件从mp3转换为wav。但我认为存在问题是因为文件名称中不能包含连字符。

谢谢所有回答的人!