POSTing with RestKit, resulting in a zero being sent

I had a problem sending NSManagedObject using RestKit 0.20pre6, where it leads to creating an entry on my Rails site with null values. I suspect it is configured, but I correctly configured my RK objects as far as I know. There are very few examples and limited support. The example looks at NSObjects, not CoreData, so I had to make some assumptions.

Here is my code for publishing and subsequent console output. The application receives objects from the server perfectly.

// 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 Matching operation to represent: {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 a = "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 displayed data on 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 When asked about the source of the map, the 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 the display 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 Initial matching operation ... 2013-01-15 23: 25: 52,972 fish [38803: 17507 ] D restkit.object_mapping: RKMappingOperation.m: 818 Ready mapping operation was successful ... 2013-01-15 23: 25: 52.973 fish [38803: 17507] D restkit.object_mapping: RKMapperOperation.m: 316 Found unchangeable value at keyPath: events 2013-01-15 23: 25: 52,973 fish [38803: 17507] D restkit.object_mapping: RKMapperOperation.m: 389 Finished execution of 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 Skip deletion of orphaned objects: performed only for GET requests. 2013-01-15 23: 25: 52,976 fish [38803: 11f03] D restkit.core_data: RKManagedObjectRequestOperation.m: 599 Update associated target (entity: Event; id: 0x9c7f710; data: {airTemp = nil; amount = nil; createdAt = "2013-01-16 04:25:52 +0000"; eventId = 54; latitude = zero; length = nil; longitude = nil; name = nil; photo = nil; thumbnail = nil; updatedAt = "2013- 01-16 04:25:52 +0000 ";}) in the context

Here is my code now that I have moved all the mappings to the application delegate and implemented RKEntityMapping. I still do not get the 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; 
+4
source share
3 answers

Having gained a better understanding of managed objects and how RestKit uses two of them, in particular, I was able to get into the right context. There are two mainQueue created with createContexts and a persistentStore context. In addition, because of this, I did not have to select an Event object in the data model and change the class from NSManagedObject to Event and RestKit, and then recognize the object as an object of the Event class.

+1
source

There is an error in RESTKit 0.20pre6 that prints the HTTP body to NSLog with a POST request as (null), even if it was an object. I corrected it in the development of the industry. https://github.com/RestKit/RestKit/pull/1139

+5
source

For NSManagedObject mappings, you must use either RKManagedObjectMapping or RKEntityMapping , depending on the version of RestKit used, and not RKObjectMapping . If you are using the latter, then it should be RKEntityMapping .

The designated initializers NSObject and NSManagedObject do not match, resulting in null values.

You can learn more about object mapping on your wiki website: Displaying RestKit objects , especially some of the master data.

+2
source

All Articles