RestKit 0.2 'NSInternalInconsistencyException', reason: 'Unable to add mapping for keyPath scheduleEntries, one already exists…'

StackOverflow https://stackoverflow.com//questions/22058927

Question

I need some help configuring RestKit 0.2x... I am returning JSON with a Schedule object and ScheduleEntries as a nested array. Sample JSON:

[{
"id": 3,
"name": "Schedule 9",
"sWeek": 9,
"sYear": 2014,
"companyId": 1,
"createdOn": "2014-02-11T22:01:33.547",
"modifiedOn": null,
"companyCode": "0001",
"isActive": true,
"notes": "This is a test note",
"scheduleEntries": [{
    "id": 15,
    "companyId": 1,
    "userId": "a7e46520-8db7-4452-821d-7938b23fcd07",
    "scheduleId": 3,
    "jobId": 5,
    "isActive": true,
    "createdOn": "2014-02-11T22:01:33.993",
    "modifiedOn": null,
    "sWeek": 11,
    "sYear": 2014,
    "startTime": "2014-03-10T08:48:00",
    "endTime": "2014-03-10T08:48:00"
}, {
    "id": 16,
    "companyId": 1,
    "userId": "a7e46520-8db7-4452-821d-7938b23fcd07",
    "scheduleId": 3,
    "jobId": 3,
    "isActive": true,
    "createdOn": "2014-02-11T22:01:34.003",
    "modifiedOn": null,
    "sWeek": 11,
    "sYear": 2014,
    "startTime": "2014-03-11T00:57:00",
    "endTime": "2014-03-11T00:57:00"
}, {....

NSManagedObjects

@interface Schedule : NSManagedObject

@property (nonatomic, retain) NSNumber * scheduleId;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * sWeek;
@property (nonatomic, retain) NSNumber * sYear;
@property (nonatomic, retain) NSNumber * companyId;
@property (nonatomic, retain) NSString * companyCode;
@property (nonatomic, retain) NSString * notes;
@property (nonatomic, retain) NSNumber * isActive;
@property (nonatomic, retain) NSDate * createdOn;
@property (nonatomic, retain) NSDate * modifiedOn;
@property (nonatomic, retain) NSSet * scheduleEntries;

@end

@implementation Schedule

@dynamic scheduleId;
@dynamic name;
@dynamic sWeek;
@dynamic sYear;
@dynamic companyId;
@dynamic companyCode;
@dynamic notes;
@dynamic isActive;
@dynamic createdOn;
@dynamic modifiedOn;
@dynamic scheduleEntries;

@end

@interface ScheduleEntry : NSManagedObject

@property (nonatomic, retain) NSNumber * scheduleEntryId;
@property (nonatomic, retain) NSNumber * companyId;
@property (nonatomic, retain) NSNumber * usrId;
@property (nonatomic, retain) NSNumber * scheduleId;
@property (nonatomic, retain) NSNumber * jobId;
@property (nonatomic, retain) NSNumber * sWeek;
@property (nonatomic, retain) NSNumber * sYear;
@property (nonatomic, retain) NSDate * startTime;
@property (nonatomic, retain) NSDate * endTime;
@property (nonatomic, retain) NSNumber * isActive;
@property (nonatomic, retain) NSDate * createdOn;
@property (nonatomic, retain) NSDate * modifiedOn;

@end

@implementation ScheduleEntry

@dynamic scheduleEntryId;
@dynamic companyId;
@dynamic usrId;
@dynamic scheduleId;
@dynamic jobId;
@dynamic sWeek;
@dynamic sYear;
@dynamic startTime;
@dynamic endTime;
@dynamic isActive;
@dynamic createdOn;
@dynamic modifiedOn;

@end

Code:

 RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];

    // Create mapping for entity
    RKEntityMapping *scheduleEntryMapping = [RKEntityMapping mappingForEntityForName:@"ScheduleEntry" inManagedObjectStore:managedObjectStore];

    scheduleEntryMapping.identificationAttributes = @[@"scheduleEntryId"];

    [scheduleEntryMapping addAttributeMappingsFromDictionary:@{
                                                  @"id"  :          @"scheduleEntryId",
                                                  @"companyId":     @"companyId",
                                                  @"usrId" :        @"usrId",
                                                  @"scheduleId" :   @"scheduleId",
                                                  @"jobId" :        @"jobId",
                                                  @"isActive":      @"isActive",
                                                  @"sWeek":         @"sWeek",
                                                  @"sYear":         @"sYear",
                                                  @"startTime":     @"startTime",
                                                  @"endTime":       @"endTime",
                                                  @"createdOn":     @"createdOn",
                                                  @"modifiedOn":    @"modifiedOn"
                                                  }];

    RKEntityMapping *scheduleMapping = [RKEntityMapping mappingForEntityForName:@"Schedule" inManagedObjectStore:managedObjectStore];

    scheduleMapping.identificationAttributes = @[@"scheduleId"];

    [scheduleMapping addAttributeMappingsFromDictionary:@{
                                                  @"id"  : @"scheduleId",
                                                  @"name":          @"name",
                                                  @"sWeek":         @"sWeek",
                                                  @"sYear":         @"sYear",
                                                  @"companyId":     @"companyId",
                                                  @"companyCode":   @"companyCode",
                                                  @"notes":         @"notes",
                                                  @"isActive":      @"isActive",
                                                  @"createdOn":     @"createdOn",
                                                  @"modifiedOn":    @"modifiedOn",
                                                  @"scheduleEntries": @"scheduleEntries"
    }];

    [scheduleMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"scheduleEntries"
                                                                                   toKeyPath:@"scheduleEntries"
                                                                                 withMapping:scheduleEntryMapping]];

This code is very close to the RestKit 0.2x example but I can't seem to get it to work. I can get a single object just fine, but when it comes to objects that have nested data I'm stuck. It has been a few days working on this with no results. The error says I'm setting the keyPath more than once but I don't see it. Any help would be appreciated.

Was it helpful?

Solution

You just need to remove @"scheduleEntries": @"scheduleEntries" from the mapping dictionary on scheduleMapping because that is added (and therefore duplicated) by the relationship mapping.

Other than that things look correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top