我似乎在将一组Core Data对象发布到服务器时遇到了一些问题。 RestKit使得GET请求变得非常容易,但是当发布时并没有那么多。
这是服务器想要JSON的方式
{
"myThingList":[
{
"id":"0",
"code":"8406014415007DC8",
"timestamp":"2012-05-11-12.30.15.505000",
"status":"0",
"userId":"8000000",
}
]
}
我在Core Data中有一个THING类,最多可容纳200个东西。我需要立即将所有这些发送到服务器。当然,我从CD中获取的NSArray只是一个东西的数组。
以下是我的代码。我得到[valueForUndefinedKey:]:这个类不是密钥值编码兼容的密钥theId。'
RKManagedObjectMapping *thingMapping = [ RKManagedObjectMapping mappingForClass:[thingMapping class]
inManagedObjectStore:self.objectManager.objectStore ];
[thingMapping mapKeyPath:@"id" toAttribute:@"theId"];
[thingMapping mapKeyPath:@"code" toAttribute:@"code"];
...... the rest of the fields in Thing
[self.objectManager.mappingProvider setMapping:thingMapping forKeyPath:@"myThingList"];
attendReadMapping.primaryKeyAttribute = @"theId";
[self.objectManager.mappingProvider setSerializationMapping:[thingMapping inverseMapping] forClass:[Things class]];
//now, we create mapping for the MySyncEntity
RKObjectMapping *syncEntityMapping = [RKObjectMapping mappingForClass:[MySyncEntity class]];
[syncEntityMapping mapKeyPath:@"mySyncArray" toRelationship:@"mySyncArray" withMapping:thingMapping];
[[self.objectManager mappingProvider] setSerializationMapping:[syncEntityMapping inverseMapping]
forClass:[MySyncEntity class]];
MySyncEntity *mySyncInstance = [[MySyncEntity alloc] init];
mySyncInstance.mySyncArray = things;
NSString *url = [NSString stringWithFormat:@"things];
if ( [things count] > 0 ) {
[self.objectManager sendObject:mySyncInstance toResourcePath:url usingBlock:^(RKObjectLoader* postLoader) {
postLoader.delegate = aDelegate;
postLoader.method = RKRequestMethodPOST;
postLoader.userData = @"howdy";
postLoader.serializationMIMEType = RKMIMETypeJSON;
[postLoader setUsername:[prefs objectForKey:@"me"]];
[postLoader setPassword:[prefs objectForKey:@"me"]];
postLoader.targetObject = nil; // Map the results back onto a new object instead of self
}]}};
答案 0 :(得分:0)
发现了这个问题。以下需要更改
** OLD **
[syncEntityMapping mapKeyPath:@"mySyncArray" toRelationship:@"mySyncArray" withMapping:thingMapping];
** NOW **
[syncEntityMapping mapKeyPath:@"myThingList" toRelationship:@"myThingList" withMapping:thingMapping];
MySyncEntity类还需要将其NSArray Prop更改为同名“myThingList”而不是“mySyncArray”
也是这个
mySyncInstance.mySyncArray = things;
需要
mySyncInstance.myThingList = things;