Вопрос

I have an iOS app that uses RestKit to sync between my Core Data model and a Rails API.

I have Game and Team entities in my Core Data model. A Game has a to-many relationship to Teams. I am trying to update the 'score' attribute of the Teams, and then I am trying to run the putObject method on my RKObjectManager by sending in the Game. The scores of the teams are not updating on the server.

If I change an attribute of the Game, e.g. the 'state', and then send in the Game with putObject, it works correctly.

Is it possible to update more than one object with putObject given that the object has nested objects inside of it? Or do I need to run putObject on the Team when I update its 'score' attribute?

Here is my mapping code for Games.

Class itemClass = [Game class];
RKEntityMapping *mapping = [RKEntityMapping
                            mappingForEntityForName:@"Game"
                            inManagedObjectStore:managedObjectStore];

mapping.identificationAttributes = @[@"gameID"];

NSDictionary *standardDict = @{@"id": @"gameID",
                               @"created_at": @"createdAt",
                               @"updated_at": @"updatedAt"};

NSDictionary *gameDict = @{@"league_id": @"leagueID",
                          @"location_id": @"locationID",
                          @"state": @"state",
                          //.... more attributes....
                          };

[mapping addAttributeMappingsFromDictionary:standardDict];
[mapping addAttributeMappingsFromDictionary:gameDict];

[mapping addConnectionForRelationship:@"league" connectedBy:@{@"leagueID": @"leagueID"}];
[mapping addConnectionForRelationship:@"location" connectedBy:@{@"locationID": @"locationID"}];


NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
NSString *keyPath = nil;
NSString *itemsPath = @"games/:gameID";
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                                                        method:RKRequestMethodAny
                                                                                   pathPattern:itemsPath
                                                                                       keyPath:keyPath
                                                                                   statusCodes:statusCodes];

[manager addResponseDescriptor:responseDescriptor];

NSString *itemPath = @"game";
RKEntityMapping *requestMapping = [mapping inverseMapping];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
                                                                               objectClass:itemClass
                                                                               rootKeyPath:itemPath
                                                                                    method:RKRequestMethodAny];
[manager addRequestDescriptor:requestDescriptor];

//route for manipulating with existing object
RKRoute *itemRoute = [RKRoute routeWithClass:itemClass pathPattern:@"games/:gameID" method:RKRequestMethodAny];
itemRoute.shouldEscapePath = YES;

[manager.router.routeSet addRoutes:@[itemRoute]];

The mapping for a Team is written basically the exact same way, except a Team has a connection to a Game based on the Game's 'gameID.' So --> [mapping addConnectionForRelationship:@"game" connectedBy:@{@"gameID": @"gameID"}];

Это было полезно?

Решение

You are using foreign key mappings on your response mapping:

[mapping addConnectionForRelationship:@"league" connectedBy:@{@"leagueID": @"leagueID"}];
[mapping addConnectionForRelationship:@"location" connectedBy:@{@"locationID": @"locationID"}];

and these are not reversed when you use inverseMapping (because they don't contain enough information to create the inverse).

So, your requestMapping needs to be explicitly updated to include a relationship mapping to tell RestKit to process the relationship and how to map the relationship contents into the resulting JSON.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top