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

Posted

技术标签:

【中文标题】RestKit 0.2 \'NSInternalInconsistencyException\',原因:\'无法为 keyPath scheduleEntries 添加映射,一个已经存在...\'【英文标题】:RestKit 0.2 'NSInternalInconsistencyException', reason: 'Unable to add mapping for keyPath scheduleEntries, one already exists...'RestKit 0.2 'NSInternalInconsistencyException',原因:'无法为 keyPath scheduleEntries 添加映射,一个已经存在...' 【发布时间】:2014-02-27 04:17:13 【问题描述】:

我需要一些帮助来配置 RestKit 0.2x... 我正在返回带有 Schedule 对象和 ScheduleEntries 作为嵌套数组的 JSON。 示例 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】:

您只需从scheduleMapping 上的映射字典中删除@"scheduleEntries": @"scheduleEntries",因为它是由关系映射添加(并因此复制)的。

除此之外,一切看起来都是正确的。

【讨论】:

非常感谢!那是票!

以上是关于RestKit 0.2 'NSInternalInconsistencyException',原因:'无法为 keyPath scheduleEntries 添加映射,一个已经存在...'的主要内容,如果未能解决你的问题,请参考以下文章

如何在 RESTkit 0.2 中设置缓存策略

使用 RestKit 0.2 从 CoreData 获取本地数据

RestKit 0.2:扁平化 JSON 中的层次结构

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

Restkit 0.2 postObject 总是返回 request.body=(null)

无法让 RestKit 0.2 发布参数