I'm pretty new to Core Data, and objective-c. I have been up and down the Core Data documentation, and don't know what I'm doing wrong here.
At runtime, I'm getting the following error when adding the NSManagedObject "ReportItem" to the NSMutableSet "reports" in the NSManagedObject "RoomItem": NSUnknownKeyException', reason: '[<RoomItem 0x747c850> valueForUndefinedKey:]: the entity RoomItem is not key value coding-compliant for the key "reports"
The error is occurring inside of the "RoomList" store class in the "creatReportForRoom" method below, which is called upon viewWillDisapear in the UIViewController where the relevant data is entered:
- (ReportItem *)creatReportForRoom:(RoomItem *)currentRoom Report:(ReportItem *)report
{
NSDate *dateCreated = [NSDate date];
ReportItem *detailItem = [NSEntityDescription insertNewObjectForEntityForName:@"ReportItem" inManagedObjectContext:context];
[detailItem setDateReportCreated:dateCreated];
NSMutableSet *reports = [currentRoom mutableSetValueForKey:@"reports"];
[reports addObject:detailItem]; //error is occurring at this line
[allReports addObject:detailItem];
return detailItem;
}
The "RoomItem" NSManagedObject files are here:
RoomItem.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface RoomItem : NSManagedObject
@property (nonatomic, strong) NSString *building;
@property (nonatomic, strong) NSString *room;
@property (nonatomic, retain) NSString *imageKey;
@property (nonatomic, retain) UIImage *buildingImage;
@property (nonatomic, strong) UIImage *buildingThumbnail;
@property (nonatomic, strong) NSData *buildingThumbnailData;
@property (nonatomic) double objectIndex;
@property (nonatomic, strong) NSDate *dateCreated;
@property (nonatomic, retain) NSMutableSet *reports;
@end
RoomItem.m
#import "RoomItem.h"
@implementation RoomItem
@dynamic building, buildingThumbnail, buildingThumbnailData, objectIndex, room, dateCreated, imageKey, buildingImage, reports;
@end
I've including the "createItem" method were the RoomItem is created and stored, in case I'm doing something wrong there:
- (RoomItem *)createItem
{
double order;
//create new roomItem
//tracks what number item it's creating
if ([allItems count] == 0) {
order = 1.0;
}
else
{
order = [[allItems lastObject] objectIndex] + 1;
}
NSLog(@"Adding after %d items, order = %.2f", [allItems count], order);
RoomItem *detailItem = [NSEntityDescription insertNewObjectForEntityForName:@"RoomItem"
inManagedObjectContext:context];
[detailItem setObjectIndex:order];
[detailItem setDateCreated:[NSDate date]];
[allItems addObject:detailItem];
return detailItem;
}
Data Model with relationships:
Relationship Diagram:
Any advice on fixing this problem would be hugely appreciated. Please let me know if there's anything I failed to include that might help.
Thanks!