gpt4 book ai didi

core-data - 核心数据关系删除规则 - Nullify + Cascade

转载 作者:行者123 更新时间:2023-12-04 17:11:47 25 4
gpt4 key购买 nike

实体:

  • 视频
  • 播放列表项
  • 播放列表

  • 关系:
  • 视频对播放列表项是可选的,删除规则为无效
  • 播放列表对至少一个播放列表项是一对多且删除规则无效
  • Video 的播放列表项需要一一删除规则无效
  • 播放列表的播放列表项需要一一删除规则无效

  • 在 Playlist 实体中,我没有暴露 playlistItem 关系/集。相反,我有一个名为“videos”的 transient 未定义属性,它是通过对播放列表项集进行排序而生成的视频实体数组。播放列表和视频实体之间没有关系。

    在托管对象上下文中保存修改后的播放列表实例时遇到问题。

    来自 playlist.m(NSManagedObject 子类):
    - (BOOL)validatePlaylistItems:(NSSet **)playlistItems error:(NSError **)outError
    {
    NSArray *currentVideos = [self videos];
    NSArray *persistedVideos = [self videosFromPlaylistItems];

    if ([currentVideos isEqual:persistedVideos]) {
    return YES;
    }

    NSManagedObjectContext *context = [self managedObjectContext];
    for (FHPlaylistItem *pi in *playlistItems) {
    [context deleteObject:pi];
    }

    NSArray *videos = [self primitiveVideos];
    NSUInteger count = [videos count];
    for (int i = 0; i < count; i++) {
    FHPlaylistItem *pi = [FHPlaylistItem playlistItemWithVideo:[videos objectAtIndex:i]
    forIndex:i
    insertIntoManagedObjectContext:[self managedObjectContext]];
    [self addPlaylistItemsObject:pi];
    [pi setPlaylist:self];
    }

    return YES;
    }

    我在这里想要完成的只是在 MOC 需要保存时更新播放列表项集,正如您所看到的,操作开销很大 - O(N * 2)。在应用程序的第一次运行期间,MOC 保存得很好。然而,随后对视频属性的更改会导致 Core Data 产生一系列错误:
    Core Data: annotation: repairing missing delete propagation for to-many relationship playlistItems on object <FHPlaylist: 0x6c27eb0> (entity: Playlist; id: 0x6c26d40 <x-coredata://BCB69D8E-8393-4A2A-AF5D-0AA1872CE2B4/Playlist/p19> ; data: {
    accountID = 0;
    lastFetched = "2012-03-31 20:05:08 +0000";
    name = Featured;
    playlistID = 1441335931001;
    playlistItems = (
    "0x6c34be0 <x-coredata:///PlaylistItem/t9EAFBC87-5E16-4053-984F-881CCD9C1F0B2>",
    "0x6c34320 <x-coredata://BCB69D8E-8393-4A2A-AF5D-0AA1872CE2B4/PlaylistItem/p16>",
    "0x6c310c0 <x-coredata:///PlaylistItem/t9EAFBC87-5E16-4053-984F-881CCD9C1F0B3>",
    "0x6c35570 <x-coredata:///PlaylistItem/t9EAFBC87-5E16-4053-984F-881CCD9C1F0B5>",
    "0x6c34ca0 <x-coredata:///PlaylistItem/t9EAFBC87-5E16-4053-984F-881CCD9C1F0B6>",
    "0x6c34310 <x-coredata://BCB69D8E-8393-4A2A-AF5D-0AA1872CE2B4/PlaylistItem/p8>",
    "0x6c28a90 <x-coredata:///PlaylistItem/t9EAFBC87-5E16-4053-984F-881CCD9C1F0B4>",
    "0x6c34350 <x-coredata://BCB69D8E-8393-4A2A-AF5D-0AA1872CE2B4/PlaylistItem/p29>",
    "0x6c34220 <x-coredata://BCB69D8E-8393-4A2A-AF5D-0AA1872CE2B4/PlaylistItem/p2>",
    "0x6c34330 <x-coredata://BCB69D8E-8393-4A2A-AF5D-0AA1872CE2B4/PlaylistItem/p20>",
    "(...and 1 more...)"
    );
    playlistType = 0;
    referenceID = OKFFeaturedPlaylist;
    shortDescrip = "This is so descriptive! OMG!";
    thumbnailURL = nil;
    videos = "(...not nil..)";
    }) with bad fault 0x6c34320 <x-coredata://BCB69D8E-8393-4A2A-AF5D-0AA1872CE2B4/PlaylistItem/p16>
    Core Data: annotation: repairing missing delete propagation for to-many relationship playlistItems on object <FHPlaylist: 0x6c27eb0> (entity: Playlist; id: 0x6c26d40 <x-coredata://BCB69D8E-8393-4A2A-AF5D-0AA1872CE2B4/Playlist/p19> ; data: {
    accountID = 0;
    lastFetched = "2012-03-31 20:05:08 +0000";
    name = Featured;
    playlistID = 1441335931001;
    playlistItems = (
    "0x6c34be0 <x-coredata:///PlaylistItem/t9EAFBC87-5E16-4053-984F-881CCD9C1F0B2>",
    "0x6c310c0 <x-coredata:///PlaylistItem/t9EAFBC87-5E16-4053-984F-881CCD9C1F0B3>",
    "0x6c35570 <x-coredata:///PlaylistItem/t9EAFBC87-5E16-4053-984F-881CCD9C1F0B5>",
    "0x6c34ca0 <x-coredata:///PlaylistItem/t9EAFBC87-5E16-4053-984F-881CCD9C1F0B6>",
    "0x6c34310 <x-coredata://BCB69D8E-8393-4A2A-AF5D-0AA1872CE2B4/PlaylistItem/p8>",
    "0x6c28a90 <x-coredata:///PlaylistItem/t9EAFBC87-5E16-4053-984F-881CCD9C1F0B4>",
    "0x6c34350 <x-coredata://BCB69D8E-8393-4A2A-AF5D-0AA1872CE2B4/PlaylistItem/p29>",
    "0x6c34220 <x-coredata://BCB69D8E-8393-4A2A-AF5D-0AA1872CE2B4/PlaylistItem/p2>",
    "0x6c34330 <x-coredata://BCB69D8E-8393-4A2A-AF5D-0AA1872CE2B4/PlaylistItem/p20>",
    "0x6c34340 <x-coredata://BCB69D8E-8393-4A2A-AF5D-0AA1872CE2B4/PlaylistItem/p27>"
    );
    playlistType = 0;
    referenceID = OKFFeaturedPlaylist;
    shortDescrip = "This is so descriptive! OMG!";
    thumbnailURL = nil;
    videos = "(...not nil..)";
    }) with bad fault 0x6c34310 <x-coredata://BCB69D8E-8393-4A2A-AF5D-0AA1872CE2B4/PlaylistItem/p8>
    Etc. Etc. Etc.

    似乎问题出在删除规则上。投诉是“缺少删除传播”,因此似乎无效是错误的选择。但是,为什么会错呢?这对我来说没有意义。 Apple's Core Data Programming Guide中级联删除规则的措辞如果我将播放列表项的删除规则设置为级联,则看起来好像我要删除视频和播放列表对象。

    Cascade - Delete the objects at the destination of the relationship



    此外,如果我在快速枚举循环中将播放列表项到视频和播放列表的关系设置为 nil,Core Data 不会提示必须修复丢失的删除传播。
    for (FHPlaylistItem *pi in *playlistItems) {
    [pi setVideo:nil];
    [pi setPlaylist:nil];
    [context deleteObject:pi];
    }

    编辑:不,手动消除关系并非在所有情况下都有效。 (也许并不意外?)

    最佳答案

    尝试在删除块之后 [[self managedObjectContext] processPendingChanges] 是否会为您解决奇怪的消息。

    当您在同一运行循环周期中继续更改托管对象上下文并且某些涉及的对象与已删除的对象有关系时,Core Data 似乎在某些情况下会感到困惑。

    问候,

    斯文。

    关于core-data - 核心数据关系删除规则 - Nullify + Cascade,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9967622/

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