Mapping RestKit with an array of complex objects

I parsed my JSON perfectly, but my server just changed to me. My JSON looked like this:

{ "blobs": [ { "createdOn": "2012-03-16T15:13:12.551Z", "description": "Fake description", "hint": "And a useless hint", "id": 400, "name": "Fake CA one", "publicId": "FF6", "type": 0 }, { "createdOn": "2012-03-16T17:33:48.514Z", "description": "No hint on this one, but it does have a description.", "hint": "Hint", "id": 402, "name": "Second fake one in CA", "publicId": "FF8", "type": 0 } ] } 

and my comparison looked like this:

 RKObjectMapping* blobMapping = [RKObjectMapping mappingForClass:[GetResponseInRegionResponse class]]; [blobMapping mapKeyPath:@"name" toAttribute:@"name"]; [blobMapping mapKeyPath:@"id" toAttribute:@"blobId"]; [blobMapping mapKeyPath:@"description" toAttribute:@"description"]; [blobMapping mapKeyPath:@"hint" toAttribute:@"hint"]; [[RKObjectManager sharedManager].mappingProvider setMapping:blobMapping forKeyPath:@"blobs"]; 

Now my server has changed and I will return it:

 { "blobsList": { "blobs": [ { "createdOn" :"2012-03-16T15:13:12.551Z", "description": "Fake description", "hint": "And a useless hint", "id": 400, "name": "Fake CA one", "publicId": "FF6", "type": 0 }, { "createdOn": "2012-03-16T17:33:48.514Z", "description": "No hint on this one, but it does have a description.", "hint": "Hint", "id": 402, "name": "Second fake one in CA", "publicId": "FF8", "type": 0 } ] } } 

So I added this to my display:

 RKObjectMapping* blobsListMapping = [RKObjectMapping mappingForClass:[GetResponseInRegionResponseList class]]; [blobsListMapping mapKeyPath:@"blobsList" toAttribute:@"blobsList"]; [[RKObjectManager sharedManager].mappingProvider setMapping:blobsListMapping forKeyPath:@"blobsList"]; 

And there are my classes:

 @interface GetResponseInRegionResponse : NSObject { NSString* name; NSString* blobId; NSString* description; NSString* hint; } @interface GetResponseInRegionResponseList : NSObject { NSArray *blobsList; } 

When I parse this JSON, I get one object that has a JKArray of 2 objects, both of which are JKDictionary objects. It’s so clear that this is my data, but it is in the form of a JKDictionary. It never matched the GetResponseInRegionResponse class!

From reading github docs, it seems like I want to use the toRelationship method for arrays, but I just don't see where to put it. If I follow the example of an β€œarticle” and try:

 [blobListMapping mapKeyPath:@"blobs" toAttribute:@"blobsList"]; [blobListMapping mapKeyPath:@"blobs" toRelationship:@"blobsList" withMapping:blobMapping]; 

I get this exception:

 2012-03-19 14:59:53.704 Ferret[8933:16303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to add mapping for keyPath blobsList, one already exists...' 

So how can I match an array complex objects inside my json?

I appreciate any help. Thanks!

+4
source share
2 answers

You tried to change only

  [[RKObjectManager sharedManager].mappingProvider setMapping:blobMapping forKeyPath:@"blobsList.blobs"]; 

to reflect the changed path to your dataset?

+2
source

The accepted answer points to version v0.2, the current version. My solution looks a little different, the code is inserted below:

 RKObjectMapping * eventMapping = [RKObjectMapping mappingForClass:[RKJMEvent class]]; [eventMapping addAttributeMappingsFromDictionary:[RKJMEvent map]]; 
 RKObjectMapping * eventResponseMapping =[RKObjectMapping mappingForClass:[RKJMEventsResponse class]]; [eventResponseMapping addAttributeMappingsFromDictionary:[RKJMEventsResponse map]]; [eventResponseMapping addRelationshipMappingWithSourceKeyPath:@"events" mapping:eventMapping]; RKResponseDescriptor *responseDescriptorEventResponse = [RKResponseDescriptor responseDescriptorWithMapping:eventResponseMapping pathPattern:API_GET_EVENTS_PATH keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; [objectManager addResponseDescriptor:responseDescriptorEventResponse]; 
+3
source

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


All Articles