title: NSManagedObject exception “this class is not key value coding-compliant” and crashes the app in ios 6 but works for ios 5
body: <p>I having an issue where my application crashes with the following exception: </p>
  
  <h2>ABC[1936:c07] <em>*</em> Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[&lt;_NSObjectID_48_0 0xb63e310> valueForUndefinedKey:]: this class is not key value coding-compliant for the key id.'</h2>
  
  <p>The strange issue with this exception is that it does not occur when using iOS5. Please see the code where the exception takes place below:</p>
  
  <pre><code>- (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 &lt; countOfQuotations; i++)
          {
              [sectionInfo insertObject:defaultRowHeight inRowHeightsAtIndex:i];
          }
  
          [infoArray addObject:sectionInfo];
      }
  
      self.sectionInfoArray = infoArray;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Would this exception be being caused because due to me having a Fetched Property defined within the class Tour that gets an array of TourDetail classes. Please see the implementation code for both classes below:</p>
  
  <pre><code>    #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 &lt;Foundation/Foundation.h&gt;
  #import &lt;CoreData/CoreData.h&gt;
  
  @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 &lt;Foundation/Foundation.h&gt;
  #import &lt;CoreData/CoreData.h&gt;
  
  
  @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
  </code></pre>
  
  <p>Any help with this issue would be greatly appreciated. As I am at a loss as to how I can fix this. </p>
  
  <p>Thanks,
  Michael</p>
  
  <p>UPDATE:</p>
  
  <p>When I remove the Fetched Property the exception does not occur with iOS6. Please see the predicate I have configured below:</p>
  
  <h2>Fetched Property tourDetails Predicate tour_id == $FETCH_SOURCE.id</h2>
  
  <p>Can you see anything I doing wrong with the setup of this predicate ? My goal is to use this so as I can return an Array of TourDetail objects for each tour_id that mathces the id column inside the Tour table.</p>
  
  <p>UPDATE:</p>
  
  <p>I have been able to diagnose that the exception is being thrown because of the Predicate as when I call both tables separately there is no exception raised. Can you see any issue with the predicate that I have created ?</p>
  
  <p>Please see code below showing how I am retrieving the objects from the Core Data DB:</p>
  
  <pre><code>- (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:&amp;error];
      }];
  }
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>The root of the problem is definitely coming from the Predicate I have defined for the Fetched property but can you advise how I should write the predicate to link between the 2 tables. As when I write the predicate tour_id == 0 and directly reference an id I know exists the fetched property works correctly. But when I use $FETCH_SOURCE.id the key value coding exception is thrown. What property do you use to reference the the table you wish to link to ?</p>
  
  <p>Really appreciate all your help with this. </p>
  
  <p>Thanks,
  Michael</p>