问题
I am using RestKit to map the following JSON to core data.
I have a many-to-many relationship set up in core data.
A hopper can have many users and a user can have many hoppers.
I am getting this error message:
(entity: Hopper; id: 0x767d240
hopperToUsers = <relationship fault: 0x8352960 'hopperToUsers' ;
So none of my users are being updated.
The 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"
}]
}]
}
My code, which is following the Managed Object Request example at the RestKit README https://github.com/RestKit/RestKit .
I realize my NSURL needs to be changed for the params, but The Hopper seems to map OK:
`[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];}`
回答1:
When you call addPropertyMapping
and create the relationship mapping, the ...FromKeyPath
relates to the JSON so it should be set to @"users"
.
来源:https://stackoverflow.com/questions/17910766/restkit-relation-mapping-many-to-many-core-data-relationship-fault-error