2012-06-18 44 views
0

我似乎在將Core數據對象數組發佈到服務器時遇到了一些問題。 RestKit使得GET請求變得非常容易,但是當涉及到發佈並不是那麼多時。Restkit:從核心數據提取到服務器的POST對象陣列

這裏是他們的方式在服務器希望JSON

{ 
    "myThingList":[ 
     { 
     "id":"0", 
     "code":"8406014415007DC8", 
     "timestamp":"2012-05-11-12.30.15.505000", 
     "status":"0", 
     "userId":"8000000", 

     } 
    ] 
} 

我對核心數據的事類,最多可容納200件事情。我需要將所有這些一次發送到服務器。當然,從CD中取出後我的NSArray只是一個THINGS的數組。

以下是我的代碼。我得到了[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

發現此問題。下面需要改變

** OLD ** 
[syncEntityMapping mapKeyPath:@"mySyncArray" toRelationship:@"mySyncArray" withMapping:thingMapping]; 


** NOW ** 
[syncEntityMapping mapKeyPath:@"myThingList" toRelationship:@"myThingList" withMapping:thingMapping]; 

MySyncEntity類也需要有它的NSArray道具改爲同名「myThingList」,而不是「mySyncArray」

而且這個

mySyncInstance.mySyncArray = things; 

需要被

mySyncInstance.myThingList = things;