RESTKit:具有一对多关系的 POST

Posted

技术标签:

【中文标题】RESTKit:具有一对多关系的 POST【英文标题】:RESTKit: POST with one-to-Many Relationship 【发布时间】:2014-04-08 04:34:02 【问题描述】:

我正在尝试以以下格式发布:

 
    "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 MeetingActivityto-many 关系

应用代理:

- (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;

****编辑****

发帖:

.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】:

您应该使用操作管理器进行 POST,而不是 http 客户端。这样,提供的要发布的对象将被映射以生成正文内容。

您缺少对象管理器的使用以及将使用的请求描述符的定义,就像您现有的响应描述符一样,以定位适当的映射。请求描述符可以使用[meetingMapping inverseMapping]

除此之外:不是 100% 清楚,但您不应该在每次做某事时都重新创建整个核心数据堆栈。 setupCoreDataWithRESTKit 应该只被调用一次,在你尝试发送任何东西/使用 Core Data 之前,然后不要再次调用。

在日志中:但是 managedObjectStore 是 nil -> 所以关于你的 Core Data 堆栈配置的某些东西不起作用,你需要调试以找出它是什么......

【讨论】:

以上是关于RESTKit:具有一对多关系的 POST的主要内容,如果未能解决你的问题,请参考以下文章

RestKit 一对多逆向关系(无CoreData)

RestKit嵌套的一对多关系映射很慢

如何保持对象一对多 RestKit

Restkit + Coredata - 仅将唯一对象添加到一对多关系中

RestKit 0.2 映射 Core Data <--> JSON 与一对多关系

如何使用 RestKit 指定关系?