- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究 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/
简短版本: -removeObserver:forKeyPath:有什么用? ? 为什么不总是使用 -removeObserver:forKeyPath:context: ? 长版 在开发 Cocoa
我是另一个初学者,试图了解 Cocoa 及其复杂性。我正在读德沃的《Objective-C》。在键值编码部分中有一些 setValue:forKeyPath: 的示例。不知何故,尽我所能,我无法让它工
我有一个 UIView 子类,它在 NSDictionary 中注册自身和一个带有对象的观察者。 当此 View 被删除时(当其包含的 View 被删除时),我在 View 的 dealloc 方法中
我创建了一个观察器来跟踪AVPlayer的“速率”。每当AVPlayer速率按预期变化时,都会显示观察者通知。但是,当我尝试在AVPlayer正在播放的项目上结束播放时删除观察者时,出现以下崩溃: *
如果我的实例正在观察另一个对象的某些属性,我是否应该在 dealloc 中调用 removeObserver:forKeyPath:? 最佳答案 是 ...除非有更合适的时间提早执行。 观察者被保存为
我一直在努力寻找示例,但我所看到的在我的案例中不起作用。 下面的代码等价于什么: object.addObserver(self, forKeyPath: "keyPath", options: [.
if (mPlayerItem){ [mPlayerItem removeObserver:self forKeyPath:kStatusKey]; [[NSNotificationC
我正在使用以下代码来了解当前播放 时间。但是当我使用 MPMusicPlayerController 播放歌曲时它不起作用。请帮我解决这个问题。任何想法或建议都将不胜感激 [self addObser
NSMutableDicitionary 的setValue 方法中的forKey 和forKeyPath 有什么区别?我在文档中查找了它们,它们对我来说似乎是一样的。我尝试了以下代码,但无法区分其中
我有一些代码大量使用 KVO,并且在多个地方有 addObserver:forKeyPath: 和 removeObserver:forKeyPath:。该应用程序偶尔会崩溃并显示“无法删除关键路径的
我在从 Xcode 的“崩溃”部分检索到以下崩溃日志时遇到了一些问题。只有少数设备受到此崩溃报告的影响。 我已经分析了问题,但我猜这是 Apple 框架上的错误。但我找不到复制它的方法。 这里有一个类
假设我有一些类似于以下内容的 KVC 兼容对象: class Person : NSObject { var office: Office var firstName: String
我遇到了一个小问题: Player *player1; Player *player2; Player *player3; Player *player4; 在 Player 类中,我有一个名为 sc
我正在研究 RestKit 关系映射示例,但无法理解这些方法调用的目的是什么,或者调用中是否存在拼写错误。他们指的是什么?对象加载器何时会在这些关键路径处遇到内容? [objectManager.ma
根据Apple和我见过的大量例子,使用KVO/KVC来观察自己是没有问题的。另外,根据这些相同的来源,通过在对象的 init 方法中使用 addObserver:forKeypath:options:
我用 .Old | 创建了一个观察者.新的 选项。在处理程序方法中,我尝试获取前后值,但编译器提示:'NSString' is not convertible to 'NSDictionaryInde
有一次我在使用 KVO 时忘记调用“removeObserver:forKeyPath:”。在iOS10模拟器上,模拟器崩溃了。但在iOS11模拟器上一切正常。没有内存泄漏,也没有崩溃。 我很困惑为什
我有一个 ViewController 类,它有一个模型属性,我想在模型属性发生变化时观察它。在我的模型对象中,我有一个属性会在我的应用程序后台定期更新。当它更新时,我需要在我的 ViewContro
我是一名优秀的程序员,十分优秀!