RESTKit:两个端点 POST、PUT、DELETE [英] RESTKit: Two EndPoints POST, PUT, DELETE

查看:50
本文介绍了RESTKit:两个端点 POST、PUT、DELETE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个端点:

http://rest.domain.com/invite

http://rest.domain.com/template

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

Depending on what options the User selects, I need to able to PUT, POST, & Delete on both EndPoints.

所有三种方法的映射略有不同.每个 EndPoint 的映射也不同.

Mapping for all three methods is slightly different. Mapping for each EndPoint is also different.

设置映射的最佳方法是什么,以便它们加载一次,并且可以根据选项(PUTPOST)为每个端点多次使用>Delete) 用户选择?我必须在一个故事板场景中完成这个!

What's the best way to setup mappings so they are loaded once and can be used multiple times for each EndPoint depending upon what option (PUT, POST, or Delete) the User selects? I have to accomplish this on one storyboard Scene!

目前,下面是我在 POST/invite 端点时使用的代码(它在我重新映射的第一个 POST b/c 后崩溃):

Currently, below is the code that I use when POSTing to the /invite EndPoint (it crashes after the first POST b/c I'm remapping):

- (void)sendInvite:(NSInteger)methodType
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    self.objectManager = [self getObjectManager];
    self.objectManager.managedObjectStore = appDelegate.managedObjectStore;
    RKEntityMapping *invitationMapping = [RKEntityMapping mappingForEntityForName:kInvite
                                                             inManagedObjectStore:self.objectManager.managedObjectStore];

    RKEntityMapping *activityMapping = [RKEntityMapping mappingForEntityForName:kActivity
                                                           inManagedObjectStore:self.objectManager.managedObjectStore];


    Invite *invitation;

    switch (methodType) {
        case POST:
        {
            invitationMapping = [RESTMappingProvider invitionPostMapping:invitationMapping];
            activityMapping =  [RESTMappingProvider activityPostMapping:activityMapping];
            [invitationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:kActivitiesRelationship
                                                                                              toKeyPath:kActivitiesRelationship
                                                                                            withMapping:activityMapping]];

            invitation = [self inviteForMethod:POST]; //This method just assigns values to the attributes

            [self setupDescriptors:invitationMapping forKeyPath:kMeetupKeyPath descriptorClassIsTemplate:NO];
            [self.objectManager.HTTPClient  registerHTTPOperationClass:[AFHTTPRequestOperation class]];
            [self.objectManager.managedObjectStore.mainQueueManagedObjectContext saveToPersistentStore:NO];

            [self.objectManager postObject:invitation path:kMeetupKeyPath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                [self removeDuplicateObjects];  //After removing relationship Dups, I save to persistent store

            } failure:^(RKObjectRequestOperation *operation, NSError *error) {
            }];

        }
            break;
        case PUTACCEPT:
        case PUTDECLINE:
        case PUTEDIT:
        {
            invitationMapping = [RESTMappingProvider invitionPutMapping:invitationMapping];
            activityMapping = [RESTMappingProvider activityPutMapping:activityMapping];
            [invitationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:kActivitiesRelationship
                                                                                              toKeyPath:kActivitiesRelationship
                                                                                            withMapping:activityMapping]];

            invitation = [self inviteForMethod:methodType]; //This method just assigns values to the attributes

            [self setupDescriptors:invitationMapping forKeyPath:kMeetupKeyPath descriptorClassIsTemplate:NO];
            [self.objectManager.HTTPClient  registerHTTPOperationClass:[AFHTTPRequestOperation class]];

            [self.objectManager putObject:invitation path:kMeetupKeyPath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
            } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                NSLog(@"Failure in PUT");
            }];
        }
            break;
    }
}

我为模板端点做了类似的事情

I do something similar for the Templates EndPoint

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

请求 &响应描述符:

Request & Response Descriptor:

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

}

我知道上面的方法是不正确的,因为在我 POST 或 PUT 后 Xcode 崩溃了.我还没有实现删除 b/c 我不知道如何正确设置.

I know the approach above is incorrect as Xcode crashes after I POST or PUT. I haven't implemented Delete yet b/c I'm not sure how to set this up correctly.

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

Do I load the mappings ONCE in viewDidLoad? Do I create PUT, POST, DELETE x 2 EndPoints = 6 RKEntityMappings?

需要一些有关最佳实践的指导.代码示例或一些分步说明会很棒!

Need some guidance on best practice. Code sample or some step-by-step instructions would be great!

推荐答案

您需要创建许多不同的映射,因为您需要处理不同的结构响应.如果一种实体类型的响应都是某个通用超集的所有子集,那么您可以只使用一个(用于映射响应,另一个用于请求).您对预期的 JSON 一无所知,所以我无法判断.

You need to create an many different mappings as you have different structure responses to process. If the responses for one entity type are all subsets of some common superset then you can use just one (for mapping responses and another for requests). You don't say anything about the expected JSON so I can't tell.

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

In your code I see 2 request descriptors and 1 response descriptor. The requests match against any method so will always be used. The response descriptor matches only GET responses so won't work for anything. You should likely have one response descriptor per end point per method (as you say they need different mappings to be applied to each).

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

viewDidLoad isn't necessarily the correct place to configure this. It looks like you have a single object manager and this configuration code should run when it is created (not when it is used, brcause the view can be loaded multiple times and the configuration would be duplicated).

这篇关于RESTKit:两个端点 POST、PUT、DELETE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆