gpt4 book ai didi

iphone - iOS RestKit [mappingProvider setMapping :forKeyPath:] method? 的目的是什么

转载 作者:行者123 更新时间:2023-11-29 13:39:22 25 4
gpt4 key购买 nike

我正在研究 RestKit 关系映射示例,但无法理解这些方法调用的目的是什么,或者调用中是否存在拼写错误。他们指的是什么?对象加载器何时会在这些关键路径处遇到内容?

[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"user"];
[objectManager.mappingProvider setMapping:taskMapping forKeyPath:@"task"];
[objectManager.mappingProvider setMapping:projectMapping forKeyPath:@"project"];

作为数据加载的 JSON 文件 有 3 个对象:项目、任务 和用户。请注意,tasks 是复数

核心数据模型中定义了 3 个实体:用户、任务和项目。这些以大写字母开头。

最后,从数据模型派生的 NSManagedObject 类 具有以下关系:Task>assignedUser 和 User>tasks 和 Project 是一个常规的 NSObject

@"task"应该是@"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

感谢您的澄清!

最佳答案

RKObjectMappingProvider setMapping:forKeyPath: 方法指示映射提供程序在遇到给定键路径中的数据时使用哪个对象映射。对于示例及其关联的 JSON 数据,没有 task 键路径的实际实例——因此在这个特定示例中,[objectManager.mappingProvider setMapping:taskMapping forKeyPath:@"task"] 语句不是必须的;在另一种情况下,你很可能有类似的东西

"task":{"id":10,"name":"Some task", "assigned_user_id":5}

在这种情况下,它是必需的。由于 [projectMapping mapRelationship:@"tasks"withMapping:taskMapping],JSON 流中的实际任务被映射。

您可以使用 RKLogConfigureByName() 来检查 RestKit 正在做什么——非常有用。 RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace) 在这种情况下将为您提供映射逻辑的“逐条”。

关于iphone - iOS RestKit [mappingProvider setMapping :forKeyPath:] method? 的目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9603758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com