Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios RESTKit:两个端点POST、PUT、DELETE_Ios_Restkit_Restkit 0.20 - Fatal编程技术网

Ios RESTKit:两个端点POST、PUT、DELETE

Ios RESTKit:两个端点POST、PUT、DELETE,ios,restkit,restkit-0.20,Ios,Restkit,Restkit 0.20,我有两个端点: 根据用户选择的选项,我需要能够在两个端点上PUT,POST,&Delete 这三种方法的映射略有不同。每个端点的映射也不同 根据用户选择的选项(PUT、POST、或Delete),设置映射的最佳方法是什么,以使映射只加载一次,并可用于每个端点多次?我必须在一个故事板场景中完成这一点 目前,以下是我在发布到/invite端点时使用的代码(在我重新映射的第一次发布b/c后崩溃): 我对模板端点执行类似的操作 - (void)saveAsTemplate { AppDele

我有两个端点:

根据用户选择的选项,我需要能够在两个端点上
PUT
POST
,&
Delete

这三种方法的映射略有不同。每个端点的映射也不同

根据用户选择的选项(
PUT
POST
、或
Delete
),设置映射的最佳方法是什么,以使映射只加载一次,并可用于每个端点多次?我必须在一个故事板场景中完成这一点

目前,以下是我在
发布
/invite
端点时使用的代码(在我重新映射的第一次发布b/c后崩溃):

我对模板端点执行类似的操作

- (void)saveAsTemplate
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    self.objectManager = [self getObjectManager];
    self.objectManager.managedObjectStore = appDelegate.managedObjectStore;

    RKEntityMapping *invitationMapping = [RKEntityMapping mappingForEntityForName:kInviteTemplates
                                                             inManagedObjectStore:self.objectManager.managedObjectStore];
    invitationMapping = [RESTMappingProvider invitionTemplateMapping:invitationMapping];


    RKEntityMapping *activityMapping = [RKEntityMapping mappingForEntityForName:kActivityTemplates
                                                           inManagedObjectStore:self.objectManager.managedObjectStore];
    activityMapping =  [RESTMappingProvider activityTemplateMapping:activityMapping];

    [invitationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:kTemplateActivitiesRelationship
                                                                                      toKeyPath:kTemplateActivitiesRelationship
                                                                                    withMapping:activityMapping]];

    STInvitesTemplate *invitation = [self templateForInvite];//this method assigns values to the attributes

    [self setupDescriptors:invitationMapping forKeyPath:kTemplatesKeyPath descriptorClassIsTemplate:YES];
    [self.objectManager.HTTPClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

    [self.objectManager postObject:invitation path:kTemplatesKeyPath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
    }];
}
请求和响应描述符:

- (void)setupDescriptors:(RKEntityMapping *)invitationMapping forKeyPath:(NSString *)keyPath descriptorClassIsTemplate:(BOOL)isTemplate
{

    RKRequestDescriptor *requestDescriptor;
    if (isTemplate)
    {
        requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[invitationMapping inverseMapping] objectClass:[Template class] rootKeyPath:nil method:RKRequestMethodAny];
    }
    else
    {
        requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[invitationMapping inverseMapping] objectClass:[Invite class] rootKeyPath:nil method:RKRequestMethodAny];
    }
    NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:invitationMapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:keyPath
                                                                                           keyPath:nil
                                                                                       statusCodes:statusCodeSet];

    self.objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
    [self.objectManager addRequestDescriptor:requestDescriptor];
    [self.objectManager addResponseDescriptor:responseDescriptor];

}
我知道上面的方法是不正确的,因为我发布或放置后Xcode崩溃。我还没有实现删除b/c我不知道如何正确设置

是否在
viewDidLoad
中加载映射一次?我是否创建
PUT、POST、DELETE
x2
EndPoints
=6
RKEntityMappings


需要一些关于最佳实践的指导。代码示例或一些分步说明将是伟大的

您需要创建许多不同的映射,因为您对流程有不同的结构响应。如果一个实体类型的响应都是某个公共超集的子集,那么您可以只使用一个(用于映射响应,另一个用于映射请求)。你没有说任何关于预期JSON的内容,所以我不能说

在代码中,我看到2个请求描述符和1个响应描述符。请求与任何方法匹配,因此将始终使用。响应描述符只匹配GET响应,因此不适用于任何情况。每个方法的每个端点都应该有一个响应描述符(正如您所说,每个方法需要应用不同的映射)


viewDidLoad
不一定是配置此功能的正确位置。看起来您有一个单一的对象管理器,此配置代码应该在创建时运行(而不是在使用时,br因为视图可以多次加载,并且配置会重复)。

谢谢。Response JSON与发送的内容相同。因此,发送的内容也会返回。删除映射会更容易吗?或者创建多个objectManager实例?两者都不是。1个管理器,可能有6个请求和响应描述符以及3个映射(如果请求与响应JSON相反,则相反)。酷。在AppDelegate中设置它可以吗?它可以工作,但不正确。应用程序委托用于应用程序级事件管理,而不是任意的数据控制操作……由于有两个实体,每个实体对应于每个端点,因此如何处理响应描述符。如何删除Objectmanager及其映射?
- (void)setupDescriptors:(RKEntityMapping *)invitationMapping forKeyPath:(NSString *)keyPath descriptorClassIsTemplate:(BOOL)isTemplate
{

    RKRequestDescriptor *requestDescriptor;
    if (isTemplate)
    {
        requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[invitationMapping inverseMapping] objectClass:[Template class] rootKeyPath:nil method:RKRequestMethodAny];
    }
    else
    {
        requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[invitationMapping inverseMapping] objectClass:[Invite class] rootKeyPath:nil method:RKRequestMethodAny];
    }
    NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:invitationMapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:keyPath
                                                                                           keyPath:nil
                                                                                       statusCodes:statusCodeSet];

    self.objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
    [self.objectManager addRequestDescriptor:requestDescriptor];
    [self.objectManager addResponseDescriptor:responseDescriptor];

}