RestKit中延迟加载的最佳实践

时间:2014-02-11 22:04:44

标签: ios objective-c restkit

我有一个API,我正在查询当我查询组织列表时,我得到了一个数组:

{ 
    id: integer,
    name: string
}

但我也能够获得每个返回单个对象的组织的详细信息,如:

{ 
    id: integer,
    name: string,
    description: text,
    visit_count: integer,
    image_url: string
}

我如何在RestKit中设置对象?

1 个答案:

答案 0 :(得分:1)

您需要设置以下内容:

  1. 包含您在第二个块中描述的所有元素的对象
  2. 对象管理器

    RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:YOUR HOST NAME]];
    objectManager.requestSerializationMIMEType=RKMIMETypeJSON;
    
  3. 包含对象中所有元素的映射对象

    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[YOUR CLASS class]];
    
  4. 对象的映射

    [mapping addAttributeMappingsFromArray:@[@"id",
                                             @"name",
                                             @"description",
                                             @"visit_count",
                                             @"image_url"
                                             ]];
    
  5. 响应描述符

    RKResponseDescriptor *responseDesciptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:OBJECT URL keyPath:YOUR KEY PATH statusCodes:nil];
    
  6. 将响应描述符添加到对象管理器

    [objectManager responseDesciptor];
    
  7. 现在你可以点击服务器了

    [[RKObjectManager sharedManager] postObject:nil path:OBJECT URL parameters:nil
         success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
             self.variable = [NSMutableArray arrayWithArray: mappingResult.array];
         }
     } failure:^(RKObjectRequestOperation *operation, NSError *error) {
    
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
    
      }];