【发布时间】:2014-04-14 00:45:47
【问题描述】:
使用 RestKit,我正在尝试映射以下 JSON。
{
"userID": 1,
"collections": [
{
"collectionID": 120,
"friends": [
{
"friendID": 6,
"orders": [
{
"orderID": 1,
"name": "Small"
}
]
}
]
},
{
"collectionID": 123,
"friends": [
{
"friendID": 6,
"orders": [
{
"orderID": 2,
"name": "Medium"
}
]
}
]
}
]
}
我正在使用 RestKit 和 MagicalRecord - 设置代码、映射和关系如下
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Model" ofType:@"momd"]];
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"db.sqlite"];
[managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:nil];
[managedObjectStore createManagedObjectContexts];
// Configure MagicalRecord to use RestKit's Core Data stack
[NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:managedObjectStore.persistentStoreCoordinator];
[NSManagedObjectContext MR_setRootSavingContext:managedObjectStore.persistentStoreManagedObjectContext];
[NSManagedObjectContext MR_setDefaultContext:managedObjectStore.mainQueueManagedObjectContext];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://127.0.0.1:3500"]];
objectManager.managedObjectStore = managedObjectStore;
RKEntityMapping *appMapping = [RKEntityMapping mappingForEntityForName:@"App" inManagedObjectStore:managedObjectStore];
appMapping.identificationAttributes = @[ @"userID" ];
RKEntityMapping *collectionMapping = [RKEntityMapping mappingForEntityForName:@"Collection" inManagedObjectStore:managedObjectStore];
collectionMapping.identificationAttributes = @[ @"collectionID" ];
[collectionMapping addAttributeMappingsFromArray:@[@"collectionID"]];
RKEntityMapping *friendMapping = [RKEntityMapping mappingForEntityForName:@"Friend" inManagedObjectStore:managedObjectStore];
friendMapping.identificationAttributes = @[ @"friendID" ];
[friendMapping addAttributeMappingsFromArray:@[@"friendID"]];
RKEntityMapping *orderMapping = [RKEntityMapping mappingForEntityForName:@"Order" inManagedObjectStore:managedObjectStore];
orderMapping.identificationAttributes = @[ @"orderID" ];
[orderMapping addAttributeMappingsFromArray:@[@"orderID", @"name"]];
RKRelationshipMapping *appCollectionRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"collections" toKeyPath:@"collections" withMapping:collectionMapping];
[appMapping addPropertyMapping:appCollectionRelationship];
RKRelationshipMapping *collectionFriendRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"friends" toKeyPath:@"friends" withMapping:friendMapping];
[collectionMapping addPropertyMapping:collectionFriendRelationship];
RKRelationshipMapping *friendsOrderRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"orders" toKeyPath:@"orders" withMapping:orderMapping];
[friendsOrderRelationship setAssignmentPolicy:RKUnionAssignmentPolicy];
[friendMapping addPropertyMapping:friendsOrderRelationship];
然后,使用下面的控制台查询我的 API 上的 /test 路由(输出上面的 JSON 块)...
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:appMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
[objectManager getObjectsAtPath:@"/test" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
App *app = [mappingResult firstObject];
for (Collection *collection in app.collections) {
NSLog(@"collectionID = %@", collection.collectionID);
for (Friend *friend in collection.friends) {
NSLog(@"friendID = %@", friend.friendID);
for (Order *order in friend.orders) {
NSLog(@"Name = %@", order.name);
}
}
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {}];
提供以下输出 - 注意两个名称都是中型,而不是一个小而另一个中型。
collectionID = 120
friendID = 6
Name = Medium
Name = Small
collectionID = 123
friendID = 6
Name = Medium
Name = Small
为什么会发生这种情况?我猜这是因为两个好友 ID 相同,即使他们在不同的收藏夹下...
【问题讨论】:
标签: objective-c core-data restkit restkit-0.20