gpt4 book ai didi

iphone - 无法在核心数据中重新排序 NSOrderedSet 关系

转载 作者:太空狗 更新时间:2023-10-30 03:28:53 25 4
gpt4 key购买 nike

有谁知道为什么我不能在 Core Data 的 NSOrderedSet 关系中保存对象顺序的更改?我已经知道使用 NSOrderedSet 关系生成的函数效果不佳的错误,因此我总是使用键路径。下面是 TableView 中两个函数的代码。

tableView:moveRowAtIndexPath:toIndexPath: 声称保存成功,但实际上更改并没有保存,意思是如果我执行[tableView reloadData];,旧的秩序还在。即使我退出应用程序并重新启动它,顺序也没有改变。我已经用多个 NSLogs 验证了这一点。第二个函数,tableView:commitEditingStyle:forRowAtIndexPath:,我用它来删除 NSOrderedSet 条目,效果很好。

我的理论是,Core Data 在其内部表示中丢弃了来自 NSOrderedSet 关系的顺序信息,因此由于对象保持不变,它认为不需要保存任何东西。有没有人经历过这样的事情?如果是这样,您找到解决方法了吗?

-(void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 
{

NSManagedObjectContext *context= [self managedObjectContext];

Playlist *playlist = (Playlist*) [context objectWithID:playlistID];


[playlist willChangeValueForKey:@"tracks"];
NSMutableOrderedSet *exchange = [playlist mutableOrderedSetValueForKey:@"tracks"];;

NSInteger fromIndex = sourceIndexPath.row;
NSInteger toIndex = destinationIndexPath.row;

NSMutableArray *arrayOfTracks = [NSMutableArray arrayWithArray:[exchange array]];

[arrayOfTracks exchangeObjectAtIndex:fromIndex withObjectAtIndex:toIndex];
[[playlist mutableOrderedSetValueForKey:@"tracks"] removeAllObjects];


[[playlist mutableOrderedSetValueForKey:@"tracks"] addObjectsFromArray:arrayOfTracks];
playlist.md5Hash = nil;
[playlist didChangeValueForKey:@"tracks"];

NSError *savingError = nil;
if ([context save:&savingError]){
NSLog(@"Successfully saved the context for reorder");
} else {
NSLog(@"Failed to save the context. Error = %@", savingError); }

}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSManagedObjectContext *context= [self managedObjectContext];

Playlist *playlist = (Playlist*) [context objectWithID:playlistID];

Track *track = [self.tracksFRC objectAtIndexPath:indexPath];


NSMutableOrderedSet *exchange = [NSMutableOrderedSet orderedSetWithOrderedSet: playlist.tracks];


[exchange removeObject:track];
[track removeBelongingPlaylistObject:playlist];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [playlist.tracks count])];

[[playlist mutableOrderedSetValueForKey:@"tracks"] replaceObjectsAtIndexes:indexSet withObjects:[exchange array]];

playlist.md5Hash = nil;

NSError *savingError = nil;
if ([context save:&savingError]){
NSLog(@"Successfully saved the context for remove entry");
} else {
NSLog(@"Failed to save the context. Error = %@", savingError); }
}
}

编辑: 我能够通过两次调用 [context save:&savingError] 来解决问题,一次是删除记录,一次是将它们重新插入新的命令。不过,这个修复应该不是真正必要的。

最佳答案

转换成数组的原因是什么? NSMutableOrderedSet 已经支持方法 exchangeObjectAtIndex:withObjectAtIndex:

我建议:

NSMutableOrderedSet *exchange = [playlist.tracks mutableCopy];

NSInteger fromIndex = sourceIndexPath.row;
NSInteger toIndex = destinationIndexPath.row;

[exchange exchangeObjectAtIndex:fromIndex withObjectAtIndex:toIndex];

playlist.tracks = exchange;

此外,您似乎在播放列表和轨道之间存在多对多关系,因此您的删除方法之所以有效,是因为您调用了 [track removeBelongingPlaylistObject:playlist]; 该方法中的其他所有内容都应该是冗余(除了保存上下文)。

关于iphone - 无法在核心数据中重新排序 NSOrderedSet 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9485764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com