所以问题是当我尝试从 here 加载实体时我无法正常工作。我的路径模式似乎是错误的。 这是我的映射和描述符:
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];
}];
}
最佳答案
从您的跟踪日志中可以看出,您的托管对象存在一些问题,而不是您的映射。您的实体显然已在模型中定义,并具有正确的名称和适当的属性。所以看起来要么您为实体创建了类但错误地创建了类,要么对象管理器没有提供/正确的对象存储引用。
您的日志包含 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
时因为这会导致模式不匹配。
关于ios - RestKit 路径模式包括 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17425058/