RestKit - 与核心数据的关系映射 - 响应错误

时间:2014-02-14 20:15:16

标签: objective-c core-data restkit

我有一个Guest实体,内部guest是City实体。我使用RKRelationshipMapping在CoreData和RestKit中设置了关系。

我已正确映射单个实体,但是当我使用RKRelationshipMapping添加城市实体时,使用@“cityId”属性调用以下方法时似乎会生成此错误:

-[RKPropertyInspector classForPropertyNamed:ofEntity:]: unrecognized selector sent to instance

我的地图代码:

RKObjectManager *objectManager   = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:self.serverBaseAddress]];
    objectManager.managedObjectStore = [RKManagedObjectStore defaultStore];

    [RKObjectManager setSharedManager:objectManager];

    RKEntityMapping *cityEMapping = [RKEntityMapping mappingForEntityForName:self.cityEntityName inManagedObjectStore:[RKManagedObjectStore defaultStore]];
    [cityEMapping addAttributeMappingsFromDictionary:@{
                                                        @"id"    : @"cityId",
                                                        @"name"  : @"name"
                                                        }];

    RKEntityMapping *guestEMapping = [RKEntityMapping mappingForEntityForName:self.entityName inManagedObjectStore:[RKManagedObjectStore defaultStore]];
    [guestEMapping addAttributeMappingsFromDictionary:@{
                                                        @"name"     : @"name",
                                                        @"photo"    : @"photo",
                                                        @"comment"  : @"comment",
                                                        @"id"       : @"guestId",
                                                        @"date"     : @"date"
                                                        }];

    [guestEMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"city"
                                                                                  toKeyPath:@"city"
                                                                                withMapping:cityEMapping]];

    guestEMapping.identificationAttributes = @[ @"guestId" ];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:guestEMapping method:RKRequestMethodAny pathPattern:self.postGetAddress keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [objectManager addResponseDescriptor:responseDescriptor];

app停止的地方:(propertyClass在propertyClass中接收0x00 = [[RKPropertyInspector sharedInspector] ...)

- (Class)classForKeyPath:(NSString *)keyPath
{
    NSArray *components = [keyPath componentsSeparatedByString:@"."];
    Class propertyClass = self.objectClass;
    for (NSString *property in components) {
        propertyClass = [[RKPropertyInspector sharedInspector] classForPropertyNamed:property ofClass:propertyClass isPrimitive:nil]; 
        if (! propertyClass) propertyClass = [[RKPropertyInspector sharedInspector] classForPropertyNamed:property ofEntity:self.entity];
        if (! propertyClass) break;
    }

    return propertyClass;
}

这是json:

    [{"id":"3","name":"Convidado3","city":{"id":"2","name":"São Paulo"},"date":"Data2","comment":"comentario3","photo":":P"},
     {"id":"2","name":"Convidado2","city":{"id":"1","name":"Rio de Janeiro"},"date":"Data2","comment":"comentario2","photo":":|"},
     {"id":"1","name":"Convidado1","city":{"id":"1","name":"Rio de Janeiro"},"date":"Data1","comment":"comentario1","photo":":)"},
     {"id":"5","name":"Convidado5","city":{"id":"4","name":"Vitória"},"date":"Data4","comment":"comentario5","photo":":K"},
     {"id":"4","name":"Convidado4","city":{"id":"3","name":"Belo Horizonte"},"date":"Data3","comment":"comentario4","photo":";)"}]

实体Guest有一个名为city的关系,它通过以下方式称为来宾的关系连接到City Entity:
客户.....市
城市< --->>客人

如果我只是删除它的关系,它就可以了,没有城市子对象...... 任何帮助或建议赞赏...

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题:

完成调试中的所有步骤后,我更改了以下行

发件人:

RKEntityMapping *cityEMapping = [RKEntityMapping mappingForEntityForName:self.cityEntityName inManagedObjectStore:[RKManagedObjectStore defaultStore]];

RKEntityMapping *cityEMapping = [RKEntityMapping mappingForEntityForName:@"City" inManagedObjectStore:[RKManagedObjectStore defaultStore]];

它有效!

我无法理解这个问题,因为我在调试期间检查了 self.cityEntityName 的值,它是 @" City" ,从技术上说它应该工作。至于行:

RKEntityMapping *guestEMapping = [RKEntityMapping mappingForEntityForName:self.entityName inManagedObjectStore:[RKManagedObjectStore defaultStore]];

工作得很好......没有任何改变,我不知道该说些什么。感谢您的建议。