这是我的设置,将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: How does one post an array of objects?
这是我的设置。
- 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;
- 发布到服务器 -
SHContactPost *post = [[SHContactPost alloc] init];
RKObjectManager* manager = [RKObjectManager sharedManager];
[manager postObject:post delegate:self];
服务器帖子和响应 SUCCESSFUL 。但是,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 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];
- 发布到服务器 -
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,否则它将无效。