[英]iOS RestKit what is the purpose of [mappingProvider setMapping:forKeyPath:] method?
I'm working through the RestKit relationship mapping example and cannot understand what is the purpose of these method calls , or if there's a typo in the calls. 我正在研究RestKit关系映射示例, 无法理解这些方法调用的目的 ,或者调用中是否有错字。 What do they refer to?
他们指的是什么? When will the object loader encounter content at these key paths?
对象加载器何时会在这些关键路径上遇到内容?
[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"user"];
[objectManager.mappingProvider setMapping:taskMapping forKeyPath:@"task"];
[objectManager.mappingProvider setMapping:projectMapping forKeyPath:@"project"];
The JSON file that is being loaded as data has 3 objects: project, tasks and user. 作为数据加载的JSON文件具有3个对象:项目, 任务和用户。 Note that tasks is plural .
注意任务是复数 。
There are 3 entities defined in the core data model : User, Task and Project. 核心数据模型中定义了 3个实体 :用户,任务和项目。 These start with capital letters.
这些以大写字母开头。
Finally, the NSManagedObject classes that are derived from the data model have relationships: Task>assignedUser and User>tasks and Project being a regular NSObject 最后,从数据模型派生的NSManagedObject类具有关系:Task> assignedUser和User> tasks和Project是常规的NSObject
Should the @"task" be @"tasks"? @“任务”应该是@“任务”吗?
@implementation RKRelationshipMappingExample
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:gRKCatalogBaseURL];
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"RKRelationshipMappingExample.sqlite"];
RKManagedObjectMapping* taskMapping = [RKManagedObjectMapping mappingForClass:[Task class]];
[taskMapping mapKeyPath:@"id" toAttribute:@"taskID"];
[taskMapping mapKeyPath:@"name" toAttribute:@"name"];
[taskMapping mapKeyPath:@"assigned_user_id" toAttribute:@"assignedUserID"];
taskMapping.primaryKeyAttribute = @"taskID"; //uniquely identifies the record for update purposes
RKManagedObjectMapping* userMapping = [RKManagedObjectMapping mappingForClass:[User class]];
[userMapping mapAttributes:@"name", @"email", nil];
[userMapping mapKeyPath:@"id" toAttribute:@"userID"];
userMapping.primaryKeyAttribute = @"userID";
[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"user"];
[objectManager.mappingProvider setMapping:taskMapping forKeyPath:@"task"];
[userMapping mapRelationship:@"tasks" withMapping:taskMapping];
[taskMapping mapRelationship:@"assignedUser" withMapping:userMapping];
[taskMapping connectRelationship:@"assignedUser" withObjectForPrimaryKeyAttribute:@"assignedUserID"];
// NOTE - Project is not backed by Core Data
RKObjectMapping* projectMapping = [RKObjectMapping mappingForClass:[Project class]];
[projectMapping mapKeyPath:@"id" toAttribute:@"projectID"];
[projectMapping mapAttributes:@"name", @"description", nil];
[projectMapping mapRelationship:@"user" withMapping:userMapping];
[projectMapping mapRelationship:@"tasks" withMapping:taskMapping];
[objectManager.mappingProvider setMapping:projectMapping forKeyPath:@"project"];
}
return self;
}
//more code
@end
Thank you for the clarification! 谢谢你的澄清!
The RKObjectMappingProvider setMapping:forKeyPath:
method instructs the mapping provider about which object mapping to use when it encounters data at a given key path. RKObjectMappingProvider setMapping:forKeyPath:
方法指示映射提供程序在给定键路径中遇到数据时要使用哪个对象映射。 In the case of the example and its associated JSON data, there are no actual instances of task
keypaths -- so in this particular example, the [objectManager.mappingProvider setMapping:taskMapping forKeyPath:@"task"]
statement is not required; 在该示例及其关联的JSON数据中,没有
task
键路径的实际实例-因此,在此特定示例中, [objectManager.mappingProvider setMapping:taskMapping forKeyPath:@"task"]
语句; in another case, you might very well have something like 在另一种情况下,您可能会遇到类似
"task":{"id":10,"name":"Some task", "assigned_user_id":5}
in which case it would be required. 在这种情况下将是必需的。 The actual tasks in the JSON stream are mapped because of the
[projectMapping mapRelationship:@"tasks" withMapping:taskMapping]
. 由于
[projectMapping mapRelationship:@"tasks" withMapping:taskMapping]
映射了JSON流中的实际任务。
You can use RKLogConfigureByName()
to inspect what RestKit is doing -- very useful. 您可以使用
RKLogConfigureByName()
检查RestKit在做什么-非常有用。 RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace)
in this case would give you a "blow-by-blow" of the mapping logic. 在这种情况下
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace)
将为您提供映射逻辑的RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace)
打击”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.