我刚刚进入objective-c,需要一些我想编写的服务类的帮助。
我想创建一个 APIService 类,它使用 restkit 并返回响应。
看起来restkit是基于块的,所以当http调用返回时,解析json并返回结果集合,我必须以某种方式让我的APIService方法返回响应。
我正在寻求有关此服务骨架结构的帮助,因为我是 Objective-c 和 restkit(使用块)的新手。
我正在看这个我想在我自己的 APIService 类中设置的例子:
- (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 再次使用块?