Im having a problem with the mapping of the: latest_update field in the JSON data.
Recieving this JS开发者_运维技巧ON data from my webservice:
{"Places":[
{"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974979a",
"timestamp": "2011-10-02 23:24:42"
},
{"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974933x",
"timestamp": "2011-10-02 23:24:42"
},
{"latest_update":"2011-10-13 12:16:17"}]
}
And a snip of the code i use for the managed mapping:
//Place mapping
if (!self.placeManagedObject){
self.placeManagedObject = [RKManagedObjectMapping
mappingForClass:[Place class]];
self.placeManagedObject.primaryKeyAttribute = @"UUID";
self.placeManagedObject.rootKeyPath = @"places";
[self.placeManagedObject mapKeyPath:@"place_ID"
toAttribute:@"UUID"];
[self.placeManagedObject mapKeyPath:@"timestamp"
toAttribute:@"timestamp"];
[self.placeManagedObject mapRelationship:@"PlaceInformation"
withMapping:self.placeInfoManagedObject];
// Register mapping with the provider - means it will look for
places in the JSON input
[objectManager.mappingProvider
setMapping:self.placeManagedObject forKeyPath:@"places"];
}
//latestDBUpdate timestamp mapping
if (!self.latestDBUpdateManagedObject){
self.latestDBUpdateManagedObject = [RKManagedObjectMapping
mappingForClass:[LatestDBUpdate class]];
self.latestDBUpdateManagedObject.primaryKeyAttribute =
@"latest_update";
self.latestDBUpdateManagedObject.rootKeyPath = @"places";
[self.latestDBUpdateManagedObject mapKeyPath:@"latest_update"
toAttribute:@"latest_update"];
// Register mapping with the provider - means it will look for
places in the JSON input
[objectManager.mappingProvider
setMapping:self.latestDBUpdateManagedObject
forKeyPath:@"latest_update"];
}
RestKit will map the Place objects correct i.e: {"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974979a", "timestamp": "2011-10-02 23:24:42" } ... into place objects, but the latest_update is not mapped into the LatestDBUpdate class object, and i cannot in any way get it to work.
I hope the someone has the answer to how it is done, because hours of searching and trying has brought me no closer to a solution. Thanx Thomas
So i finally figured it out. Basically some small tweaks, and a couple of errors on my behalf:-/
So i changed the JSON output from my webservice to this formatting:
{"Places":[
{"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974979a",
"timestamp": "2011-10-02 23:24:42"
},
{"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974933x",
"timestamp": "2011-10-02 23:24:42"
}],
"Timestamps":[
{"latest_update":"2011-10-13 12:16:17"}]
I had a an error in the mapping og Timestamps -> latest_update, so i fixxed that:
//latestDBUpdate timestamp mapping
if (!self.latestDBUpdateManagedObject){
self.latestDBUpdateManagedObject = [RKManagedObjectMapping mappingForClass:[LatestDBUpdate class]];
self.latestDBUpdateManagedObject.primaryKeyAttribute = @"latest_update";
self.latestDBUpdateManagedObject.rootKeyPath = @"Timestamps";
[self.latestDBUpdateManagedObject mapKeyPath:@"latest_update" toAttribute:@"latest_update"];
// Register mapping with the provider - means it will look for the keyword Timestamps in the JSON input
[self.objectManager.mappingProvider setMapping:self.latestDBUpdateManagedObject forKeyPath:@"Timestamps"];
And then i had a major error in my get method. Basically i told RestKit that the response should only be mapped by my Place mapping and thus not check all other data in the JSON input for other keywords. Just to show the difference i present both GET methods i used. The first will ONLY map with the specified mapping scheme, and number two will check with all registred mapping schemes:
GET method number 1: (That does not work in the above context!)
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:gPlaceListBaseUrl objectMapping:self.placeManagedObject delegate:self];
GET method numer 2: (Here all mapping schemes are checked and both Place objects are mapped, as well as my LatestDBUpdate object!)
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:gPlaceListBaseUrl delegate:self];
Hopefully someone might need this someday:-D
Thomas
精彩评论