Question

I'm trying to POST in the following format:

{ 
    "meetingType": 1,
    "startDate": "2011-05-26T07:56:00.123Z",
    "recipientId": "useme",
    "comment": "haha",
     "activities": [
       {"activityType": "walking", "startTime": "2011-05-26T07:00:00.123Z", "name":"someTrail", "address":"New York", "customData":"someData"},
        {"activityType": "hiking", "startTime": "2011-05-26T07:30:00.123Z", "name":"Park", "address":"New York", "customData": {"someVar":"someData"}  }
     ]
}

I have two entities: Meeting & Activity Meeting has to-many relationship with Activity

App Delegate:

- (RKManagedObjectStore *)setupCoreDataWithRESTKit
{
     NSError * error;
    NSURL * modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"App" ofType:@"momd"]];
    NSManagedObjectModel * managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
    self.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

    [self.managedObjectStore createPersistentStoreCoordinator];

    NSArray * searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentPath = [searchPaths objectAtIndex:0];
    NSPersistentStore * persistentStore = [self.managedObjectStore addSQLitePersistentStoreAtPath:[NSString stringWithFormat:@"%@/CoreData.sqlite", documentPath] fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];

    if(!persistentStore){
        NSLog(@"Failed to add persistent store: %@", error);
    }

    [self.managedObjectStore createManagedObjectContexts];

    [RKManagedObjectStore setDefaultStore:self.managedObjectStore];
    return self.managedObjectStore;

}

****EDIT****

POSTING:

.m

@property (strong, nonatomic) RKObjectManager *objectManager;

- (RKObjectManager *)setupObjectManager
{

    NSURL *baseURL = [NSURL URLWithString:@"http://www.domain.com"];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:baseURL];
    RKObjectManager *manager = [[RKObjectManager alloc]initWithHTTPClient:httpClient];
    [manager.HTTPClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [manager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
    [manager.HTTPClient setParameterEncoding:AFJSONParameterEncoding];
    [RKMIMETypeSerialization registeredMIMETypes];
    [RKObjectManager setSharedManager:manager];

    return [RKObjectManager sharedManager];

}

- (RKObjectManager *)getObjectManager
{
    self.objectManager = (!self.objectManager) ?  [self setupObjectManager] : self.objectManager;
    return self.objectManager;
}


-(IBAction)postMeeting:(UIButton *)sender
{
    STAppDelegate *appDelegate = (STAppDelegate *)[[UIApplication sharedApplication] delegate];
    self.objectManager = [self getObjectManager];

    self.objectManager.managedObjectStore = [appDelegate setupCoreDataWithRESTKit];

    RKEntityMapping *meetingMapping = [RKEntityMapping mappingForEntityForName:@"Meeting" inManagedObjectStore:self.objectManager.managedObjectStore];
    meetingMapping.identificationAttributes = @[@"meetingId"];
    [meetingMapping addAttributeMappingsFromDictionary:@{
                                                         @"recipientId" : @"recipientId",
                                                         @"meetingType" : @"meetingType",
                                                         @"startDate" : @"startDate",
                                                         @"comment" : @"comment"
                                                         }];

    RKEntityMapping *activityMapping = [RKEntityMapping mappingForEntityForName:@"Activity" inManagedObjectStore:self.objectManager.managedObjectStore];
    activityMapping.identificationAttributes = @[@"activityId"];
    [activityMapping addAttributeMappingsFromDictionary:@{
                                                          @"name" : @"name",
                                                          @"address" : @"address",
                                                          @"startTime" : @"startTime",
                                                          @"customData" : @"customData"
                                                          }];

    [meetingMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"activities" toKeyPath:@"activities" withMapping:activityMapping]];

    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://www.domain.com"]];
    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[meetingMapping inverseMapping] objectClass:[Meeting class] rootKeyPath:@"activities" method:RKRequestMethodAny];

    [manager addRequestDescriptor:requestDescriptor];

    Meeting *meet = [NSEntityDescription insertNewObjectForEntityForName:@"Meeting"
                                                       inManagedObjectContext:self.objectManager.managedObjectStore.mainQueueManagedObjectContext];

    meet.recipientId = @"1111";
    meet.meetingType = @"meetingTest";
    meet.startDate = @"March 08";
    meet.comment = @"comment";    

    [manager postObject:meet path:@"/meeting" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        NSLog(@"success");
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"Failusre");
    }];

}

Log:

2014-04-08 11:09:32.712 App[21801:60b] W restkit:RKObjectManager.m:587 Asked to create an `RKManagedObjectRequestOperation` object, but managedObjectStore is nil.
2014-04-08 11:09:32.715 App[21801:60b] I restkit.network:RKObjectRequestOperation.m:180 POST 'http://www.domain.com/activities'
2014-04-08 11:09:33.435 App[21801:f03] E restkit.network:RKObjectRequestOperation.m:576 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1016 "Expected content type {(
    "application/x-www-form-urlencoded",
    "application/json"
)}, got text/html" UserInfo=0xef530a0 {NSLocalizedRecoverySuggestion=<html>Your request timed out.   Please retry the request.                                                                                                                                                                                                                                                                                                                                                                                                                                                                  </html>, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0xcd94790> { URL: http://www.domain.com/meetups }, NSErrorFailingURLKey=http://www.domain.com/meetups, NSLocalizedDescription=Expected content type {(
    "application/x-www-form-urlencoded",
    "application/json"
)}, got text/html, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xef484c0> { URL: http://www.domain.com/invite } { status code: 408, headers {
    "Cache-Control" = "no-cache";
    "Content-Length" = 514;
    "Content-Type" = "text/html";
    Pragma = "no-cache";
    "Proxy-Connection" = Close;
} }}
2014-04-08 11:09:33.435 App[21801:f03] E restkit.network:RKObjectRequestOperation.m:243 POST 'http://www.domain.com/invite' (408 Request Timeout / 0 objects) [request=0.7188s mapping=0.0000s total=0.7227s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1016 "Expected content type {(
    "application/x-www-form-urlencoded",
    "application/json"
)}, got text/html" UserInfo=0xef530a0 {NSLocalizedRecoverySuggestion=<html>Your request timed out.   Please retry the request.                                                                                                                                                                                                                                                                                                                                                                                                                                                                  </html>, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0xcd94790> { URL: http://www.domain.com/activities }, NSErrorFailingURLKey=http://www.domain.com/activities, NSLocalizedDescription=Expected content type {(
    "application/x-www-form-urlencoded",
    "application/json"
)}, got text/html, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xef484c0> { URL: http://www.domain.com/activities } { status code: 408, headers {
    "Cache-Control" = "no-cache";
    "Content-Length" = 514;
    "Content-Type" = "text/html";
    Pragma = "no-cache";
    "Proxy-Connection" = Close;
} }}
2014-04-08 11:09:33.436 App[21801:60b] Failusre
Was it helpful?

Solution

You should be using the operation manager to POST, not the http client. In this way the supplied object to be posted will be mapped to produce the body content.

You're missing the usage of the object manager and the definition of the request descriptor that will be used, like your existing response descriptor, to locate the appropriate mapping. The request descriptor can use [meetingMapping inverseMapping].

Aside: it isn't 100% clear if you are, but you shouldn't be recreating the entire core data stack each time you do something. setupCoreDataWithRESTKit should only be called once, before you try to send anything / use Core Data, then not called again.

In the log: but managedObjectStore is nil -> so something about your Core Data stack config isn't working, you will need to debug to work out what it is...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top