我正在使用RestKit将以下JSON映射到核心数据。
我在核心数据中设置了多对多关系。
料斗可以有很多用户,用户可以有多个料斗。
我收到此错误消息:
(entity:Hopper; id:0x767d240
hopperToUsers = <relationship fault: 0x8352960 'hopperToUsers' ;
所以我的用户都没有被更新。
json:
{
"hoppers": [{
"bearing": 0,
"created_at": "2013-07-26T07:57:00Z",
"distance": 0.0,
"id": 4,
"lat": "30.422032",
"lng": "-86.617069",
"name": "Fort Walton Beach",
"updated_at": "2013-07-26T07:57:00Z",
"users": [{
"avatar_url": null,
"created_at": "2013-07-26T21:37:21Z",
"id": 1,
"link": "http:/example.com/savetheday",
"name": "Clark Kent",
"post": " I once reported a heinous crime here. Superman came to the rescue!",
"thumbnail_url": null,
"updated_at": "2013-07-26T21:37:21Z"
}, {
"avatar_url": null,
"created_at": "2013-07-26T21:37:57Z",
"id": 2,
"link": "http:/example.com/villaincaught",
"name": "Lex Luther",
"post": " I got busted here. Almost took over the world!",
"thumbnail_url": null,
"updated_at": "2013-07-26T21:37:57Z"
}]
}]
}
我的代码,它遵循RestKit自述文件https://github.com/RestKit/RestKit中的托管对象请求示例。
我意识到我的NSURL需要为params改变,但是Hopper似乎映射好了:
`[managedObjectStore createManagedObjectContexts];
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
[userMapping addAttributeMappingsFromDictionary:@{ @"avatar_url": @"avatarURL" ,
@"created_at": @"createdAt",
@"link": @"jsonURL",
@"post": @"post",
@"thumbnail_url": @"thumbnailURL",
@"updated_at": @"updatedAt",
@"id": @"userID"}];
RKEntityMapping *hopperMapping = [RKEntityMapping mappingForEntityForName:@"Hopper" inManagedObjectStore:managedObjectStore];
[hopperMapping addAttributeMappingsFromDictionary:@{ @"id": @"hopperID",
@"lat": @"lat",
@"lng": @"lng",
@"name": @"name",
@"created_at": @"createdAt",
@"distance": @"distance"}];
[hopperMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"hopperToUsers" toKeyPath:@"hopperToUsers" withMapping:userMapping]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:hopperMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"hoppers" statusCodes:statusCodes];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:3000/hoppers.json?lat=30.422032&lng=-86.617069"]];
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
operation.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
operation.managedObjectCache = managedObjectStore.managedObjectCache;
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
Hopper *hopper= [result firstObject];
NSLog(@"Mapped the hopper: %@", hopper);
NSLog(@"Mapped the user: %@", [hopper.hopperToUsers anyObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failed with error: %@", [error localizedDescription]);
}];
NSOperationQueue *operationQueue = [NSOperationQueue new];
[operationQueue addOperation:operation];}`
答案 0 :(得分:1)
当您致电addPropertyMapping
并创建关系映射时,...FromKeyPath
与JSON相关,因此应将其设置为@"users"
。