如何将restkit调用包装到服务类中,并集成到控制器中?

时间:2013-05-20 20:28:25

标签: objective-c restkit soa

我刚刚进入objective-c,需要一些我想写的服务类的帮助。

我想创建一个APIService类,它使用restkit并返回响应。

看起来restkit基于块,所以当http调用返回时,解析json并返回结果集合,我必须以某种方式让我的APIService方法返回响应。

我正在寻找有关此服务的骨架结构的帮助,因为我是objective-c和restkit(使用块)的新手。

我正在查看这个我想在自己的APIService类中设置的示例:

https://github.com/RestKit/RestKit/blob/development/Examples/RKTwitter/Classes/RKTwitterViewController.m#L18

- (void)loadTimeline
{
    // Load the object model via RestKit
    RKObjectManager *objectManager = [RKObjectManager sharedManager];

    [objectManager getObjectsAtPath:@"/status/user_timeline/RestKit"
                         parameters:nil
                            success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                              NSArray* statuses = [mappingResult array];
                              NSLog(@"Loaded statuses: %@", statuses);
                              _statuses = statuses;
                              if(self.isViewLoaded)
                                [_tableView reloadData];
                            }
                            failure:^(RKObjectRequestOperation *operation, NSError *error) {
                              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                               message:[error localizedDescription]
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"OK"
                                                                     otherButtonTitles:nil];
                              [alert show];
                              NSLog(@"Hit error: %@", error);
                            }];
}

有人可以帮助我充实这个骨架结构,方法调用就像时间轴一样,这就是我现在所拥有的:

@interface MyApiService : NSObject
{
  @property (nonatomic, strong, readwrite) RKObjectManager *rkObjectManager;
  - (id)initWithRKObjectManager:(RKObjectManager *)rkObjectManager;
  - (NSArray) loadTimeline:
}

@implementation MyApiService 
{
  - (id)initWithRKObjectManager:(RKObjectManager *)rkObjectManager
  {
     self = [super init];
     if(self) {
       self.rkObjectManager = rkObjectManager;
       // ...

     }
  }


  // how to define method for loadTimelines when the call returns using a block?
}

然后我会像这样使用它:

// Initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
MyAPiService *service = [[MyApiService alloc] initWithRKObjectManager: objectManager];

NSArray *statuses = [service loadTimeLine];

但不确定我是否会调用loadTimeLine,因为restkit再次使用块?

1 个答案:

答案 0 :(得分:1)

RestKit也使用委托。这是我的方式:

在.h文件中导入#import <RestKit/RestKit.h>

实施委托RKObjectLoaderDelegate

从这样的服务加载对象。

RKObjectManager *manager = [RKObjectManager sharedManager];
[manager loadObjectsAtResourcePath:@"//status/user_timeline/RestKit" delegate:self];

实施委托方法:

- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects {
// Do whatever with objects here...
}

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
    NSLog(@"Error POSTing object : %@", error);
}

RKObjectLoaderDelegate有许多有用的委托方法。

您可以关注一个不错的RestKit教程here