Note that there are some explanatory texts on larger screens.

plurals
  1. PORestkit: Post Json Array and Map response to Managed Objects
    primarykey
    data
    text
    <p>Here is my setup, Post a list of objects in Json to server and got back with updated results that will be mapped to core data and updated.</p> <p>-- Post Boday --</p> <pre><code>{ "memberId": "1000000", "contacts": [ { "phoneNumber": "+12233333333", "firstName": "john", "lastName": "H" }, { "phoneNumber": "+12244444444", "firstName": "mary", "lastName": "K" } ] } </code></pre> <p>-- Post Response --</p> <pre><code>{ "contacts": [ { "phoneNumber": "+12233333333", "firstName": "john", "lastName": "k", "isMember": "yes" }, { "phoneNumber": "+12244444444", "firstName": "mary", "lastName": "k", "isMember": "no" } ] } </code></pre> <p>I found another thread which discusses very similar to my case. <a href="https://stackoverflow.com/q/7726437/772481">RestKit: How does one post an array of objects?</a></p> <p>This is my setup.</p> <p>-- SHContact.h --</p> <pre><code>@interface SHContact : NSManagedObject {} @property (nonatomic, strong) NSString* phoneNumber; @property (nonatomic, strong) NSString* firstName; @property (nonatomic, strong) NSString* lastName; @property (nonatomic, strong) NSNumber* isMember; @end </code></pre> <p>-- SHContactPost.m --</p> <pre><code>#import "SHContactPost.h" @implementation SHContactPost @synthesize contacts; @synthesize memberId; - (NSArray*)contacts { return &lt;list-of-SHContact&gt;; } - (NSString *)memberId { return @"my-member-id"; } @end </code></pre> <p>-- RK Mapping --</p> <pre><code>// Setup our object mappings for SHContact RKManagedObjectMapping* contactMapping = [RKManagedObjectMapping mappingForClass:[SHContact class] inManagedObjectStore:objectManager.objectStore]; contactMapping.primaryKeyAttribute = @"phoneNumber"; [contactMapping mapAttributes:@"phoneNumber", @"firstName", @"lastName", @"isMember", nil]; [objectManager.mappingProvider setObjectMapping:contactMapping forKeyPath:@"contacts"]; [[objectManager mappingProvider] setSerializationMapping:[contactMapping inverseMapping] forClass:[SHContact class]]; RKObjectMapping *cPostMapping = [RKObjectMapping mappingForClass:[SHContactPost class]]; [cPostMapping mapKeyPath:@"contacts" toRelationship:@"contacts" withMapping:contactMapping]; [cPostMapping mapAttributes:@"memberId", nil]; [objectManager.mappingProvider setSerializationMapping:[cPostMapping inverseMapping] forClass:[SHContactPost class]]; [objectManager.router routeClass:[SHContactPost class] toResourcePath:@"/contacts" forMethod:RKRequestMethodPOST]; objectManager.serializationMIMEType = RKMIMETypeJSON; </code></pre> <p>-- Post to server --</p> <pre><code>SHContactPost *post = [[SHContactPost alloc] init]; RKObjectManager* manager = [RKObjectManager sharedManager]; [manager postObject:post delegate:self]; </code></pre> <p>Server post and response is <strong>SUCCESSFUL</strong>. However, Reskit was not able to map the result back to the list of SHContact objects. This is the exception. Am I missing something?</p> <pre><code>restkit.network:RKObjectLoader.m:222 Encountered errors during mapping: Cannot map a collection of objects onto a non-mutable collection. Unexpected destination object type 'SHContactPost' </code></pre> <p><strong>UPDATE:</strong> This is what I changed to make it work for me.</p> <p>-- RK Mapping --</p> <pre><code>// Setup our object mappings for SHContact RKManagedObjectMapping* contactMapping = [RKManagedObjectMapping mappingForClass:[SHContact class] inManagedObjectStore:objectManager.objectStore]; contactMapping.primaryKeyAttribute = @"phoneNumber"; [contactMapping mapAttributes:@"phoneNumber", @"firstName", @"lastName", @"isMember", nil]; [objectManager.mappingProvider addObjectMapping:contactMapping]; </code></pre> <p>-- Post to server --</p> <pre><code>SHContactPost *post = [[SHContactPost alloc] init]; RKObjectManager* manager = [RKObjectManager sharedManager]; RKObjectMapping* responseMapping = [manager.mappingProvider objectMappingForClass:[SHContact class]]; [manager postObject:post usingBlock:^(RKObjectLoader *loader) { loader.delegate = self; loader.targetObject = nil; responseMapping.rootKeyPath = @"contacts"; loader.objectMapping = responseMapping; }]; </code></pre> <p><strong>loader.targetObject needs to set it to nil, otherwise, it won't work.</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload