【问题标题】:get method using RKObjectManager instead of RKRequestOperation - RestKit使用 RKObjectManager 而不是 RKRequestOperation 获取方法 - RestKit
【发布时间】:2014-07-26 13:13:19
【问题描述】:

以下是使用 RKObjectRequestOperation 获取方法的函数,如何使用 RKObjectManager 编写相同的方法

 NSString *hostName = [NSString stringWithFormat:@"%@",[self getHostName]];

NSString *urls = [[NSString alloc] initWithFormat:@"%@/randomcollection/%@/0/%li",hostName,profileId,(long)noOfItems];


RKObjectMapping *responseMapping = [MappingProvider collectionMapping ];


RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    NSURL *URL = [NSURL URLWithString:urls];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly timeoutInterval:90.0f];
[request setValue:[GameManager sharedInstance].authenticatedUser.appTokenId forHTTPHeaderField:@"tokenId"];

RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];


[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    if (success) {
        RKLogInfo(@"Load collection: %@", mappingResult.array);
    }
} failure:^(RKObjectRequestOperation *operation, NSError *error) {

    if (failure) {
        failure(operation, error);
    }

}];

[objectRequestOperation start];

编辑:这是我尝试过的,它给了我 * 断言失败的错误 -[RKRouter URLWithRoute:object:]

      NSString *hostName = [NSString stringWithFormat:@"%@",[self getHostName]];  
   NSString* urls = [[NSString alloc]      initWithFormat:@"%@/userprofile/selectbyid/%@",hostName,profileId];

 RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:urls]];
    [manager.HTTPClient setDefaultHeader:@"tokenId" value:[GameManager sharedInstance].authenticatedUser.appTokenId];

RKObjectMapping *responseMapping = [MappingProvider userProfileRespMapping ];


    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    [manager addResponseDescriptor:responseDescriptor];


    UserProfile * modelObject = [[UserProfile alloc] init];
    [modelObject setProfileId:profileId];

    [[RKObjectManager sharedManager] getObject:modelObject
                                          path:nil
                                    parameters:nil
                                       success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                                  if (success) {
                                                       RKLogInfo(@"Load collection of Userprofiles: %@", mappingResult.array);
                                                       NSLog(@"Mapping Results %@",[mappingResult.array firstObject]);
                                                    UserProfile *currentUserProfile = (UserProfile *)[mappingResult.array firstObject];
                                                       success(currentUserProfile);
                                                   }
                                       }
                                       failure:^(RKObjectRequestOperation *operation, NSError *error) {

                                        if (failure) {
                                                       RKLogError(@"Operation failed with error: %@", error);
                                                       failure(operation, error);
                                                   }
                                       }];

【问题讨论】:

  • 尝试编写对象管理器版本,如果它不起作用,请显示您尝试过的内容并解释它做错了什么
  • @ok wain 我会写下编辑问题
  • @Wain : 请检查上面我写的函数,它在路由函数中给了我错误

标签: ios objective-c restkit restkit-0.20


【解决方案1】:

这很简单。它应该看起来像这样。这将为您提供一个起点。要获得更详细的答案,您应该具体化您的问题。

// Initial setup
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"your base url"]];

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:modelClass];

[mapping addAttributeMappingsFromDictionary:@{@"sourceProperty" : @"destinationProperty"}];

[manager addResponseDescriptor:
               [RKResponseDescriptor
                   responseDescriptorWithMapping:mapping
                                          method:RKRequestMethodGET
                                     pathPattern:@"yourPath"
                                         keyPath:@"jsonStructureDepending"
                                     statusCodes:
                                         RKStatusCodeIndexSetForClass(
                                             RKStatusCodeClassSuccessful)]];

[[manager router].routeSet
                addRoutes:@[
                       [RKRoute routeWithClass:[model class]
                                   pathPattern:@"yourPath"
                                        method:RKRequestMethodGET]];


//Within your controller or elsewhere
[[RKObjectManager sharedManager] getObject:modelObject
                                      path:nil
                                parameters:nil
                                   success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                   }
                                   failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                   }];

【讨论】:

  • 谢谢我已经尝试过基于此请检查我编辑的问题我在路由功能上遇到错误
  • 我已经更新了我的答案忘了提到如果你想通过modelObject get,你需要添加一个路由。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多