RestKit路径模式,包括ID

时间:2013-07-02 11:59:42

标签: ios restkit-0.20

所以问题是,当我尝试从here加载实体时,我没有让事情发挥作用。我的Pathpatterns似乎错了。 这是我的映射和描述符:

RKEntityMapping *statsMapping = [RKEntityMapping mappingForEntityForName:@"Stat" inManagedObjectStore:managedObjectStore];

[statsMapping addAttributeMappingsFromDictionary:@{
 @"sort_id" : @"sortID",
 @"id" : @"statID",
 @"deleted" : @"deletedFlag",
 @"created_at": @"createdAt",
 @"updated_at": @"updatedAt"
 }];
statsMapping.identificationAttributes = @[ @"statID" ];
[statsMapping addAttributeMappingsFromArray:@[ @"title"]];


RKEntityMapping *featuresMapping = [RKEntityMapping mappingForEntityForName:@"Feature" inManagedObjectStore:managedObjectStore];

[featuresMapping addAttributeMappingsFromDictionary:@{
 @"sort_id" : @"sortID",
 @"id" : @"featureID",
 @"deleted" : @"deletedFlag",
 @"created_at": @"createdAt",
 @"updated_at": @"updatedAt",
 }];
featuresMapping.identificationAttributes = @[ @"featureID" ];
[featuresMapping addAttributeMappingsFromArray:@[ @"title", @"value"]];
[statsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"features" toKeyPath:@"features" withMapping:featuresMapping]];

RKResponseDescriptor *statsDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:statsMapping
                                                                                  pathPattern: @"/api/cars/:carID/features.json"
                                                                                      keyPath:nil
                                                                                  statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptorsFromArray:@[newsDescriptor, catalogDescriptor, statsDescriptor]];

所以当我使用pathPattern:nil它可以工作,但如果url没有返回任何答案,它只是尝试将另一个responsedescriptor放到响应中并给我随机数据:)

问题是,如果我在车型中间有车牌ID,我应该如何申报呢?

谢谢!

Edit1:这就是我的要求:

    - (void)getStats:(NSNumber *)carID
    {
    [[RKObjectManager sharedManager] getObjectsAtPath:[NSString stringWithFormat:@"api/cars/%@/features.json", carID]
                                           parameters:@{@"auth_token" : [Constants authToken]}
                                              success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                                  RKLogInfo(@"Load complete: Stats loaded");
                                              }
                                              failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                                  RKLogError(@"Load failed with error: %@", error);
                                                  [self showError:error withDelay:3];
                                              }];
}

1 个答案:

答案 0 :(得分:3)

从跟踪日志中,您的托管对象存在一些问题,而不是您的映射。您的实体显然在模型中定义,并且具有正确的名称和适当的属性。所以它看起来要么为实体创建了类,要么错误地为对象管理器提供了/正确的对象存储引用。


您的日志包含CoreData: error: Failed to call designated initializer on NSManagedObject class 'Stat'[<Stat 0xa68b7c0> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "statID".,这些都与实体创建/引用有关。目前尚不清楚这是如何根据发布的代码发生的。

尝试更改路径模式以删除前导斜杠:

@"api/cars/:carID/features.json"

定义statsDescriptor时可能导致模式不匹配。