抛出异常观察NSManagedObjectContext保存

时间:2015-06-30 23:56:58

标签: ios core-data

我收到以下异常An observer of NSManagedObjectContextDidSaveNotification illegally threw an exception. Objects saved = { inserted = "{(\n)}"; updated = "{(\n <FJCDSeriesPreferences: 0x1744b30e0> (entity: SeriesPreferences; id: 0xd000000000700012 <x-coredata://29345DF0-9D92-410F-9305-84BFA1672BB9/SeriesPreferences/p28> ; data: {\n isFavourite = 0;\n region = us;\n series = \"0xd00000000d840010 <x-coredata://29345DF0-9D92-410F-9305-84BFA1672BB9/Series/p865>\";\n seriesID = 955129;\n})\n)}"; } and exception = [<FJCDSeriesPreferences 0x1744b30e0> valueForUndefinedKey:]: the entity SeriesPreferences is not key value coding-compliant for the key "program". with userInfo = { NSTargetObjectUserInfoKey = "<FJCDSeriesPreferences: 0x1744b30e0> (entity: SeriesPreferences; id: 0xd000000000700012 <x-coredata://29345DF0-9D92-410F-9305-84BFA1672BB9/SeriesPreferences/p28> ; data: {\n isFavourite = 0;\n region = us;\n series = \"0xd00000000d840010 <x-coredata://29345DF0-9D92-410F-9305-84BFA1672BB9/Series/p865>\";\n seriesID = 955129;\n})"; NSUnknownUserInfoKey = program; }

以下是导致问题的代码

-(void)contextDidUpdate:(NSNotification *)note{
    NSManagedObjectContext *incomingContext = [note object];
    NSDictionary *userInfo = [[note userInfo] copy];
    if (self.context != incomingContext){
        //Do not track
        return;
    }
predicateWithFormat:@"program.objectId IN %@", _objectsToFetch];
    NSPredicate *programIDPredicate = [NSPredicate predicateWithFormat:@"%K like %@", @"program.objectId", _objectsToFetch ];

    NSSet *inserted = [userInfo objectForKey:@"inserted"];
    if ([inserted count]){
        //objects have been inserted
        self.insertedObjectsBlock([inserted allObjects]);

    }

    NSSet *deleted = [[[userInfo objectForKey:@"deleted"] copy ]filteredSetUsingPredicate:programIDPredicate];
    if ([deleted count]){
        //objects have been deleted
        self.deletedObjectsBlock([deleted allObjects]);
    }

    NSSet *updated = [[[userInfo objectForKey:@"updated"] copy]filteredSetUsingPredicate:programIDPredicate];
    if ([updated count]){
        //objects have been updated
        self.updatedObjectsBlock([updated allObjects]);
    }   
}

问题出现在谓词周围,因为并非所有正在更新键值的实体都是IM试图访问的属性。 IM尝试做的是查找已更改的对象并查看它们是否与特定关系相关,在这种情况下,查看首选项是否与程序相关。

1 个答案:

答案 0 :(得分:2)

您的谓词存在问题。如果不能直接使用调试器进行戳,看起来您正在对不响应这些方法的对象调用program.objectId。如果您尝试访问NSManagedObjectID,那么您的拼写错误,因为该方法为objectID而不是objectId

为了理解这一点,我建议在使用谓词之前设置一个断点并打印出每个集合中的内容,并确认你得到了你期望获得的内容。

顺便说一句,那些对-copy的调用是多余的,浪费。