Restkit: Post Json Array and Map response to Managed Objects

為{幸葍}努か 提交于 2019-12-21 23:19:05

问题


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.

-- Post Boday --

{
    "memberId": "1000000",
    "contacts": [
        {
            "phoneNumber": "+12233333333",
            "firstName": "john",
            "lastName": "H"
        },
        {
            "phoneNumber": "+12244444444",
            "firstName": "mary",
            "lastName": "K"
        }
    ]
}

-- Post Response --

{
    "contacts": [
        {
            "phoneNumber": "+12233333333",
            "firstName": "john",
            "lastName": "k",
            "isMember": "yes"

        },
        {
            "phoneNumber": "+12244444444",
            "firstName": "mary",
            "lastName": "k",
            "isMember": "no"
        }
    ]
}

I found another thread which discusses very similar to my case. RestKit: How does one post an array of objects?

This is my setup.

-- SHContact.h --

@interface SHContact : NSManagedObject {}

@property (nonatomic, strong) NSString* phoneNumber;
@property (nonatomic, strong) NSString* firstName;
@property (nonatomic, strong) NSString* lastName;
@property (nonatomic, strong) NSNumber* isMember;
@end

-- SHContactPost.m --

#import "SHContactPost.h"
@implementation SHContactPost
@synthesize contacts;
@synthesize memberId;
- (NSArray*)contacts {
    return <list-of-SHContact>;
}
- (NSString *)memberId {
    return @"my-member-id";
}
@end

-- RK Mapping --

// 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;

-- Post to server --

SHContactPost *post = [[SHContactPost alloc] init];
RKObjectManager* manager = [RKObjectManager sharedManager];
[manager postObject:post delegate:self];

Server post and response is SUCCESSFUL. However, Reskit was not able to map the result back to the list of SHContact objects. This is the exception. Am I missing something?

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'

UPDATE: This is what I changed to make it work for me.

-- RK Mapping --

// 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];

-- Post to server --

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;
 }];

loader.targetObject needs to set it to nil, otherwise, it won't work.

来源:https://stackoverflow.com/questions/11373448/restkit-post-json-array-and-map-response-to-managed-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!