I am having a problem posting an NSManagedObject using RestKit 0.20pre6 where it results in creating an entry on my Rails site with nil values. I suspect the mapper but I have set up my RK objects to the best of my knowledge correctly. There are very few examples and limited support. The example deals with NSObjects and not CoreData so I've had to make some assumptions.
Here is my code for posting and followed by the console output. The app gets objects from the server fine.
// Sync the added object to the server
// ****************************************
RKObjecMapping *responseMapping = [RKObjectMapping mappingForClass:[Event class]];
// RKEntityMapping *responseMapping = [RKEntityMapping mappingForEntityForName:@"Event" inManagedObjectStore:self.managedObjectStore];
[responseMapping addAttributeMappingsFromDictionary:@{
@"id": @"eventId",
@"name": @"name",
@"amount": @"amount",
@"length": @"length",
@"updated_at": @"updatedAt",
@"created_at": @"createdAt",
@"latitude": @"latitude",
@"longitude": @"longitude",
@"thumbnail": @"thumbnail",
@"airTemp": @"airTemp",
}];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *eventDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
pathPattern:@"/events"
keyPath:@"events"
statusCodes:statusCodes
];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
[requestMapping addAttributeMappingsFromDictionary:@{
@"id": @"eventId",
@"name": @"name",
@"amount": @"amount",
@"length": @"length",
@"updated_at": @"updatedAt",
@"created_at": @"createdAt",
@"latitude": @"latitude",
@"longitude": @"longitude",
@"thumbnail": @"thumbnail",
@"airTemp": @"airTemp",
}];
// For any object of class Article, serialize into an NSMutableDictionary using the given mapping and nest
// under the 'article' key path
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[Event class]
rootKeyPath:@"events"
];
LocationsAppDelegate *appDelegate = (LocationsAppDelegate *)[[UIApplication sharedApplication] delegate];
// RKObjectManager *manager = [RKObjectManager managerWithBaseURL:gBaseURL];
[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;
[[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor];
[[RKObjectManager sharedManager] addResponseDescriptor:eventDescriptor];
NSLog(@"RVC-addEvent %@ - %@", appDelegate.user.login, appDelegate.user.password);
[[NBEventManager sharedManager] setUsername:appDelegate.user.login andPassword:appDelegate.user.password];
appDelegate.objectManager.HTTPClient = [NBEventManager sharedManager];
event = [eventsArray objectAtIndex:0];
[[RKObjectManager sharedManager] postObject:event
path:@"/events"
parameters:nil
success:nil
failure:nil
];
}
2013-01-15 23:25:52.857 fish[38803:13403] I restkit.network:RKHTTPRequestOperation.m:152 POST 'http://localhost:3000/events' 2013-01-15 23:25:52.969 fish[38803:16903] I restkit.network:RKHTTPRequestOperation.m:179 POST 'http://localhost:3000/events' (201 Created) [0.1121 s] 2013-01-15 23:25:52.971 fish[38803:17507] D restkit.object_mapping:RKMapperOperation.m:360 Executing mapping operation for representation: { event = { airTemp = ""; amount = ""; "created_at" = "2013-01-16T04:25:52Z"; id = 54; latitude = ""; length = ""; longitude = ""; name = ""; thumbnail = ""; "updated_at" = "2013-01-16T04:25:52Z"; "user_id" = ""; }; } and targetObject: (entity: Event; id: 0x9c7f710 ; data: { airTemp = 0; amount = nil; createdAt = "2013-01-16 04:25:52 +0000"; eventId = 0; latitude = "28.158466"; length = 0; longitude = "-82.509215"; name = nil; photo = nil; thumbnail = nil; updatedAt = nil; }) 2013-01-15 23:25:52.971 fish[38803:17507] D restkit.object_mapping:RKMapperOperation.m:287 Found mappable data at keyPath 'event': { airTemp = ""; amount = ""; "created_at" = "2013-01-16T04:25:52Z"; id = 54; latitude = ""; length = ""; longitude = ""; name = ""; thumbnail = ""; "updated_at" = "2013-01-16T04:25:52Z"; "user_id" = ""; } 2013-01-15 23:25:52.971 fish[38803:17507] D restkit.object_mapping:RKMapperOperation.m:232 Asked to map source object { airTemp = ""; amount = ""; "created_at" = "2013-01-16T04:25:52Z"; id = 54; latitude = ""; length = ""; longitude = ""; name = ""; thumbnail = ""; "updated_at" = "2013-01-16T04:25:52Z"; "user_id" = ""; } with mapping eventId>", " name>", " amount>", " length>", " updatedAt>", " createdAt>", " latitude>", " longitude>", " airTemp>", " thumbnail>" )> 2013-01-15 23:25:52.971 fish[38803:17507] D restkit.object_mapping:RKMappingOperation.m:754 Starting mapping operation... 2013-01-15 23:25:52.972 fish[38803:17507] D restkit.object_mapping:RKMappingOperation.m:818 Finished mapping operation successfully... 2013-01-15 23:25:52.973 fish[38803:17507] D restkit.object_mapping:RKMapperOperation.m:316 Found unmappable value at keyPath: events 2013-01-15 23:25:52.973 fish[38803:17507] D restkit.object_mapping:RKMapperOperation.m:389 Finished performing object mapping. Results: { event = " (entity: Event; id: 0x9c7f710 ; data: {\n airTemp = nil;\n amount = nil;\n createdAt = \"2013-01-16 04:25:52 +0000\";\n eventId = 54;\n latitude = nil;\n length = nil;\n longitude = nil;\n name = nil;\n photo = nil;\n thumbnail = nil;\n updatedAt = \"2013-01-16 04:25:52 +0000\";\n})"; } 2013-01-15 23:25:52.973 fish[38803:13403] D restkit.core_data:RKManagedObjectRequestOperation.m:534 Skipping deletion of orphaned objects: only performed for GET requests. 2013-01-15 23:25:52.976 fish[38803:11f03] D restkit.core_data:RKManagedObjectRequestOperation.m:599 Refreshing mapped target object (entity: Event; id: 0x9c7f710 ; data: { airTemp = nil; amount = nil; createdAt = "2013-01-16 04:25:52 +0000"; eventId = 54; latitude = nil; length = nil; longitude = nil; name = nil; photo = nil; thumbnail = nil; updatedAt = "2013-01-16 04:25:52 +0000"; }) in context
Here is my code now as I have moved all of the mappings to the app delegate and have implemented RKEntityMapping. I still get no JSON object attached to the POST call.
RKEntityMapping *eventMapping = [RKEntityMapping mappingForEntityForName:@"Event" inManagedObjectStore:managedObjectStore];
[eventMapping addAttributeMappingsFromDictionary:@{
@"id": @"eventId",
@"name": @"name",
@"amount": @"amount",
@"length": @"length",
@"updated_at": @"updatedAt",
@"created_at": @"createdAt",
@"latitude": @"latitude",
@"longitude": @"longitude",
@"thumbnail": @"thumbnail",
@"airTemp": @"airTemp",
}];
eventMapping.identificationAttributes = @[ @"eventId" ];
//RKRelationshipMapping *eventRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"events"
// toKeyPath:@"events"
// withMapping:eventMapping];
//[eventMapping addPropertyMapping:eventRelationship];
// Register our mappings with the provider
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:eventMapping
pathPattern:nil
keyPath:@"event"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
RKEntityMapping *eventRequestMapping = [eventMapping inverseMapping];
RKRequestDescriptor *eventRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:eventRequestMapping
objectClass:[Event class]
rootKeyPath:@"event"
];
[objectManager addRequestDescriptor:eventRequestDescriptor];
[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;
There is a bug in RESTKit 0.20pre6, which print HTTP Body to NSLog with POST request as (null), even if it were an object. I fixed it in develop branch. https://github.com/RestKit/RestKit/pull/1139
For NSManagedObject
mappings you should use either RKManagedObjectMapping
or RKEntityMapping
, depending on the version of RestKit you are using, instead of the RKObjectMapping
. If you are using the last one then it should be RKEntityMapping
.
The designated initialisers of NSObject
and NSManagedObject
are not the same, thus resulting in null values.
You can read more about object mapping on their wiki site: RestKit Object Mapping especially the Core Data part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With