The NSManagedObject exception “this class does not meet the key encoding requirements” and causes the application to crash in ios 6 but works for ios 5

I had a problem when my application crashes with the following exception:

ABC [1936: c07] * Application terminated due to an unmapped exception "NSUnknownKeyException", reason: '[<_NSObjectID_48_0 0xb63e310> valueForUndefinedKey:]: this class is not a key value compatible with the code for the key identifier.'

A strange problem with this exception is that this does not happen when using iOS5. See Code in which an exception occurs below:

- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if ((self.sectionInfoArray == nil) || ([self.sectionInfoArray count] != [self numberOfSectionsInTableView:self.tableView])) { NSMutableArray *infoArray = [[NSMutableArray alloc] init]; for (Tour *tour in self.tours) { SectionInfo *sectionInfo = [[SectionInfo alloc] init]; sectionInfo.tour = tour; sectionInfo.open = NO; NSLog(@"Tour Details Count %@", [[tour tourDetails] objectAtIndex:0]); NSNumber *defaultRowHeight = [NSNumber numberWithInteger:DEFAULT_ROW_HEIGHT]; NSInteger countOfQuotations = [[sectionInfo.tour tourDetails] count]; for (NSInteger i = 0; i < countOfQuotations; i++) { [sectionInfo insertObject:defaultRowHeight inRowHeightsAtIndex:i]; } [infoArray addObject:sectionInfo]; } self.sectionInfoArray = infoArray; } 

}

Whether this exception was raised because I got the Fetched property defined in the Tour class, which receives an array of TourDetail classes. See the implementation code for both classes below:

  #import "Tour.h" #import "TourDetail.h" @implementation Tour @dynamic background_url; @dynamic id; @dynamic summary; @dynamic title; @dynamic tour_tourdetail; @dynamic tourDetails; @end #import "TourDetail.h" #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> @class TourDetail; @interface Tour : NSManagedObject @property (nonatomic, retain) NSString * background_url; @property (nonatomic, retain) NSNumber * id; @property (nonatomic, retain) NSString * summary; @property (nonatomic, retain) NSString * title; @property (nonatomic, retain) TourDetail *tour_tourdetail; @property (nonatomic, retain) NSArray *tourDetails; #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> @interface TourDetail : NSManagedObject @property (nonatomic, retain) NSString * audiofile; @property (nonatomic, retain) NSString * detail; @property (nonatomic, retain) NSNumber * id; @property (nonatomic, retain) NSNumber * lattitude; @property (nonatomic, retain) NSNumber * longitude; @property (nonatomic, retain) NSString * title; @property (nonatomic, retain) NSNumber * tour_id; @property (nonatomic, retain) NSManagedObject *tourdetail_tour; @end @implementation TourDetail @dynamic audiofile; @dynamic detail; @dynamic id; @dynamic lattitude; @dynamic longitude; @dynamic title; @dynamic tour_id; @dynamic tourdetail_tour; @end 

Any help in this matter would be greatly appreciated. Since I do not understand how I can fix this.

Thanks Michael

UPDATE:

When I remove the Fetched property, an exception does not occur with iOS6. See the predicate that I configured below:

Selected property tourDetails Predicate tour_id == $ FETCH_SOURCE.id

Do you see something that I am doing wrong with setting up this predicate? My goal is to use this so that I can return an array of TourDetail objects for each tour_id that computes the id column inside the Tour table.

UPDATE:

I was able to diagnose that the exception is thrown due to the Predicate, because when I call both tables separately, the exception does not occur. Can you see any problems with the predicate that I created?

Refer to the code below showing how I retrieve objects from the underlying DB:

 - (void)viewDidLoad { [super viewDidLoad]; [DrivingToursContent setupStaticData]; self.tableView.sectionHeaderHeight = HEADER_HEIGHT; _openSectionIndex = NSNotFound; self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"custombackground.ptoung"]]; self.managedObjectContext = [[BaseCoreDataController sharedInstance] newManagedObjectContext]; [self loadRecordsFromCoreData]; [self loadRecordsFromCoreDataForTourDetail]; NSLog(@"Tour Detail array count: %d", [self.toursTest count]); // Do any additional setup after loading the view. } - (void)loadRecordsFromCoreData { [self.managedObjectContext performBlockAndWait:^{ [self.managedObjectContext reset]; NSError *error = nil; NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([Tour class])]; [request setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"id" ascending:YES]]]; self.tours = [self.managedObjectContext executeFetchRequest:request error:&error]; }]; } 

UPDATE:

The root of the problem definitely comes from the Predicate that I defined for the Fetched property, but can you tell me how I should write the predicate to link between the two tables. Like when I write the predicate tour_id == 0 and directly refer to the identifier that, as I know, exists, the corrected property works correctly. But when I use $ FETCH_SOURCE.id, a key value encoding exception is thrown. What property do you use to refer to the table to which you want to bind?

In fact, appreciate all your help.

Thanks Michael

+8
ios iphone core-data
source share
5 answers

without seeing your code, you call a method in a class that is deprecated in iOS6, so it crashes.

0
source share

The code you post for viewWillAppear doesn't say anything that affects Core Data in any obvious way, which makes it pretty hard to guess what is going on. Some of these objects may be managed objects, but who knows which ones or how you created them?

However, the error message gives a huge key:

 [<_NSObjectID_48_0 0xb63e310> valueForUndefinedKey:]: this class is not key value coding-compliant for the key id. 

The fact that this mention of _NSObjectID_48_0 indicates that at some point you are using instances of NSManagedObjectID when you expect instances of NSManagedObject . NSManagedObjectID does not have a property called id , regardless of how the object looks, so requesting one of its id will result in an error with an error that is not compatible with the key value.

As for why this happens, it's still impossible to say, since you haven't posted any code that gives any information about how you use Core Data.

may have something to do with the predicate:

 tour_id == $FETCH_SOURCE.id 

If you built your selection so that $FETCH_SOURCE was NSManagedObjectID , that would be a problem. It is possible (and I can only guess) that you use it when you request the result type NSManagedObjectIDResultType .

0
source share

@property (non-atomic, persistent) NSNumber * id;

Not id reserved word in Objective-C? Have you tried renaming this property to something else to see if it causes conflicts?

0
source share

Without delving into your code, NSFetchRequest can set the return type of the result, for example, NSManagedObject ID ResultType or NSManagedObjectResultType.

Requires NSManagedObjectResultType, but NSManagedObjectIDResultType is returned.

0
source share

I have the same problem when I use MagicalRecord, but have no problem when using Xcode to generate CoreData source code.

0
source share

All Articles