Search code examples
iosobjective-crestkit

Pull 2 APIs with RestKit


I need help pulling in two APIs with RestKit.

I have API1 modeled, and pulling in correctly already.

The problem is trying to figure out how to pull API2 in to the ViewController.

Specifically, I already have the model class set up, but in the ViewController where the results of API1 + API2 will display, I can't figure out how to work it into my viewDidLoad.

Thanks!

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // RestKit
    NSString *baseURL = @"http://api.firstwebsite.com/v1";
    RKObjectManager *manager = [RKObjectManager sharedManager];

    if (!manager) {
        manager = [RKObjectManager objectManagerWithBaseURLString:baseURL];
        manager.client.serviceUnavailableAlertEnabled = YES;
        manager.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
    } else {
        manager.client.baseURL = [RKURL URLWithString:baseURL];
    }

    return YES;
}

WebListViewController.m

@property (strong, nonatomic) NSArray *hArray;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:
    [NSString stringWithFormat:
    @"/something/?limit=100&something=%@&something=%@&apikey=xxxx", var1, var2]
    usingBlock:^(RKObjectLoader *loader) {
        loader.onDidLoadObjects = ^(NSArray *objects){

            hArray = objects;

            [_tableView reloadData];

        };
        [loader.mappingProvider setMapping:[Fe mapping] forKeyPath:@"fe"];
        loader.onDidLoadResponse = ^(RKResponse *response){
            //NSLog(@"BodyAsString: %@", [response bodyAsString]);
        };
    }];
}

Solution

  • Abstract your view controller (and app delegate) away from knowledge of where the data is coming from - they have no business knowing. The app delegate shouldn't really know anything about any of this. The view controller should know that data exists and that additional data can be requested, but this should be in terms of the internal app data model, not the external model or the source of the data.

    So, create a data controller. Usually a singleton. Provide an interface to get / set / request / update the data model based on set criteria. Treat all calls as asynchronous with completion callback blocks.

    Internally, this data controller can manage multiple object managers, each with a different base URL , mappings and descriptors, but this is all internal knowledge.