2015-06-30 32 views
0

我得到下面的异常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; }抛出异常观测的NSManagedObjectContext节省

这是导致该问题是围绕谓词中出现的问题

-(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属性的键值。即时消息试图做的是找到已更改的对象,并查看它们是否与特定关系相关,在这种情况下查看首选项是否与程序相关。

+0

'_objectsToFetch'的类型是什么? –

+0

_objectsToFetch是一个包含我想要获取的对象的对象ID的数组。 – TheM00s3

回答

1

您的谓词存在问题。如果不能直接使用调试器戳它,看起来您在对这些方法没有响应的对象上调用program.objectId。如果您尝试访问NSManagedObjectID,那么您的输入方式为objectID而不是objectId

为了解决这个问题,我建议在使用谓词之前先设置一个断点,然后打印出每个集合中的内容并确认您获得了期望得到的结果。

顺便说一句,那些拨打-copy的电话是多余的和浪费的。