Restkit:邮政JSON数组和地图管理对象响应(Restkit: Post Json Array

2019-07-30 06:52发布

这里是我的设置,发布以JSON到服务器对象的列表,并与将被映射到核心数据和更新,更新后的结果回来。

- 发布Boday -

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

- 投递响应 -

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

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

我发现另一个线程,其中讨论非常相似,我的情况。 RestKit:一个人如何发布对象的数组?

这是我的设置。

- 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映射 -

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

- 发布到服务器 -

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

服务器的帖子及回复是成功的。 然而,RESKIT无法结果映射回SHContact对象的列表。 这是个例外。 我缺少的东西吗?

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'

更新:这是我改变,使之为我工作。

- RK映射 -

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

- 发布到服务器 -

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需要将其设置为nil,否则将无法正常工作。

文章来源: Restkit: Post Json Array and Map response to Managed Objects