我正在尝试使用来自新 RestKit 0.9.3 的示例 RKCatalog。在 RKRelationshipMappingExample 中,创建者忘记在 Task 和 User 之间添加连接代码,但即使添加后也无法正常工作。
{"project": {
"id": 123,
"name": "Produce RestKit Sample Code",
"description": "We need more sample code!",
"user": {
"id": 1,
"name": "Blake Watters",
"email": "blake@twotoasters.com"
},
"tasks": [
{"id": 1, "name": "Identify samples to write", "assigned_user_id": 1},
{"id": 2, "name": "Write the code", "assigned_user_id": 1},
{"id": 3, "name": "Push to Github", "assigned_user_id": 1},
{"id": 4, "name": "Update the mailing list", "assigned_user_id": 1}
]
}}
[taskMapping connectRelationship:@"assignedUser" withObjectForPrimaryKeyAttribute:@"assignedUserID"];
- (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 setPrimaryKeyAttribute:@"taskID"];
[taskMapping mapKeyPath:@"name" toAttribute:@"name"];
[taskMapping mapKeyPath:@"assigned_user_id" toAttribute:@"assignedUserID"];
[taskMapping connectRelationship:@"assignedUser" withObjectForPrimaryKeyAttribute:@"assignedUserID"];
[objectManager.mappingProvider setMapping:taskMapping forKeyPath:@"task"];
RKManagedObjectMapping* userMapping = [RKManagedObjectMapping mappingForClass:[User class]];
[userMapping mapAttributes:@"name", @"email", nil];
[userMapping mapKeyPath:@"id" toAttribute:@"userID"];
[userMapping setPrimaryKeyAttribute:@"userID"];
[userMapping mapRelationship:@"tasks" withMapping:taskMapping];
[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"user"];
// 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;
}
我做的一切都好吗?-- 它不通过用户 ID 将任务连接到用户。