RestKit 0.2'NSInternalInconsistencyException',原因:'无法为keyPath scheduleEntries添加映射,一个已经存在......'

时间:2014-02-27 04:17:13

标签: ios json restkit restkit-0.20

我需要一些帮助配置RestKit 0.2x ...我将JSON与Schedule对象和ScheduleEntries作为嵌套数组返回。 示例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

代码:

 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]];

此代码非常接近RestKit 0.2x示例,但我似乎无法使其工作。我可以很好地得到一个对象,但是当涉及到具有嵌套数据的对象时,我就会被卡住。已经有几天没有结果了。错误说我不止一次设置keyPath但我没有看到它。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

您只需要从@"scheduleEntries": @"scheduleEntries"上的映射字典中删除scheduleMapping,因为关系映射会添加(因此重复)。{/ p>

除此之外看起来不错。