RestKit: creating stubs for foreign keys

I am trying to match foreign key relationships returned by my JSON API using RestKit.

In particular, I use Loopback to create an API with objects of type Teamand User. By default, there are two endpoints that return the following JSON:

/ Teams /

[
  {
    "title": "Team Title",
    "id": 1,
  }
]

/ Users /

[
  {
    "name": "Alice",
    "id": 1,
    "teamId": 1,
  },
  {
    "name": "Bob",
    "id": 2,
    "teamId": 1,
  }, ...
]

API, ? RestKit , (, RestKit/CoreData JSON , RestKit, Restkit: GET , Core Data) , .

, API. Team User, . , , , , , Loopback API .

, ( , ) :

Team

RKEntityMapping *teamMapping = [RKEntityMapping mappingForEntityForName:@"Team" inManagedObjectStore:objectManager.managedObjectStore];
[teamMapping addAttributeMappingsFromArray:@[ @"title", @"id" ]];
teamMapping.identificationAttributes = @[ @"id" ];
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:teamMapping method:RKRequestMethodAny pathPattern:@"Teams" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

User

RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:objectManager.managedObjectStore];
[userMapping addAttributeMappingsFromArray:@[ @"name", @"id", @"teamId" ]];
userMapping.identificationAttributes = @[ @"id" ];
[userMapping addConnectionForRelationship:@"team" connectedBy:@{ @"teamId": @"id" }];
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:userMapping method:RKRequestMethodGET pathPattern:@"Users" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

(, ), RKConnectionDescription teamId. JSON, Team, User ( Path pathPattern ), , . , , , . , , Stub id, .

RestKit?


: JSON "teamId": 1 "team": { "id": 1 } keyPath Team? , Loopback. ?


2: , :

RKEntityMapping *teamStubMapping = [RKEntityMapping mappingForEntityForName:@"Team inManagedObjectStore:objectManager.managedObjectStore];
[teamStubMapping addAttributeMappingsFromDictionary:@{@"teamId": @"id"}];
teamStubMapping.identificationAttributes = @[ @"id" ];
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:teamStubMapping method:RKRequestMethodAny pathPattern:@"Users" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

, , . ? , , ? User, .


3: , RestKit , , , , GitHub. nil :

RKEntityMapping *teamStubMapping = [RKEntityMapping mappingForEntityForName:@"Team inManagedObjectStore:objectManager.managedObjectStore];
[teamStubMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"id"]];
teamStubMapping.identificationAttributes = @[ @"id" ];
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:teamStubMapping method:RKRequestMethodAny pathPattern:@"Users" keyPath:@"teamId" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
+4
1

, , , , .

, , nil keypath mapping @"teamId". RestKit , .


JSON . , teamStubMapping, , teamMapping, id teamId. @"Users".

+2

Source: https://habr.com/ru/post/1548086/


All Articles