RESTKit:具有一对多关系的POST

时间:2014-04-08 04:34:03

标签: ios core-data restkit restkit-0.20

我尝试按以下格式发布信息:

{ 
    "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"}  }
     ]
}

我有两个实体:Meeting& Activity Meetingto-many

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");
    }];

}

日志:

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

1 个答案:

答案 0 :(得分:1)

您应该使用操作管理器来POST,而不是http客户端。通过这种方式,将映射要发布的提供对象以生成正文内容。

您将错过对象管理器的使用以及将使用的请求描述符的定义(如现有响应描述符),以找到适当的映射。请求描述符可以使用[meetingMapping inverseMapping]

除此之外:如果你是,那么它不是100%清晰,但你不应该在每次做某事时重新创建整个核心数据堆栈。在您尝试发送任何内容/使用Core Data之前,只应调用setupCoreDataWithRESTKit一次,然后再调用。

在日志中:但是managedObjectStore是nil - &gt;所以你的核心数据堆栈配置不起作用,你需要进行调试才能弄清楚它是什么......