これが私の設定です。Jsonのオブジェクトのリストをサーバーに投稿すると、コアデータにマッピングされて更新される更新された結果が返されます。
-ポストボデイ-
{
"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に設定する必要があります。そうしないと、機能しません。