3

所以问题是当我试图从这里加载实体时,我没有让事情正常工作。我的路径模式似乎是错误的。这是我的映射和描述符:

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 放入响应并给我随机数据:)

问题是,如果我在图案中间有车牌,我应该怎么申报呢?

谢谢!

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

1 回答 1

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"

在定义你的statsDescriptoras 时可能会导致模式不匹配。

于 2013-07-02T19:06:32.447 回答