ios - RESTKit:具有一对多关系的 POST

标签 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 MeetingActivity 有对多关系

应用委托(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;

****编辑****

发布:

.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

最佳答案

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

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

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

在日志中:但 ManagedObjectStore 为零 -> 所以您的核心数据堆栈配置的某些内容不起作用,您需要调试才能找出它是什么......

关于ios - RESTKit:具有一对多关系的 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22927621/

相关文章:

ios - GPUImageStillCamera 不工作

iOS NSUserDefaults 检查 float 键是否存在

iOS 为什么我的 UIPickerview 方法循环?

ios - 从这里抛出的错误不会在 swift 2 xcode 7 中处理

swift - 通知扩展访问核心数据

ios - CoreData 可转换 : Custom Transformer never gets called - NSKeyedArchiver is used instead

ios - 休息套件 : mapping with nested object with no id (dupplicated)

objective-c - iOS:RestKit 加载对象并发送参数

ios - 从另一个 viewController 快速检测 - iOS swift

iOS 请求服务器一夜之间没有响应 RestKit