我正在通過RestKit關係映射示例工作,並且無法理解這些方法調用的目的是什麼,或者如果在調用中存在拼寫錯誤。他們指的是什麼?對象加載器何時會在這些關鍵路徑上遇到內容?iOS RestKit [mappingProvider setMapping:forKeyPath:]方法的目的是什麼?
[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"user"];
[objectManager.mappingProvider setMapping:taskMapping forKeyPath:@"task"];
[objectManager.mappingProvider setMapping:projectMapping forKeyPath:@"project"];
正被加載數據的JSON文件有3個對象:項目,任務和用戶。請注意,任務是複數。
在覈心數據模型中定義了3個實體:用戶,任務和項目。這些以大寫字母開頭。
最後,從數據模型得出的NSManagedObject類有關係:任務> assignedUser和用戶>任務和項目作爲一個普通的NSObject
如若@「任務」是@「任務」?
@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
謝謝您的澄清!