Note that there are some explanatory texts on larger screens.

plurals
  1. PORestKit: How to batch multiple requests and get a response once they finish?
    primarykey
    data
    text
    <p>I just found out RestKit and it will be an important part of the app I'm doing. At the time, I was able to integrate it with the core data, but have not figured out the best way to send multiple GET requests.</p> <p>What I need to do is:</p> <p>Get data from the following addresses:</p> <pre><code>http://url.com/api/banner/ http://url.com/api/category/ http://url.com/api/link/ </code></pre> <p>The URL will always be in the following format: <a href="http://url.com/api/SOMETHING/" rel="noreferrer">http://url.com/api/SOMETHING/</a></p> <p>Once <em>all requests</em> are finished, I would like to run a code (such as calling a new view controller). What would be the best way to do this?</p> <p>At the moment, this is the code I'm using:</p> <pre><code>- (id)init { self = [super init]; if (self) { [self setupConnector]; [self setupDatabase]; [self setupMappings]; [self sendRequests]; } return self; } - (void)setupConnector { // Initialize RestKIT RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://baseURL"]]; self.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:[[NLCoreData shared] managedObjectModel]]; objectManager.managedObjectStore = self.managedObjectStore; } - (void)setupDatabase { NSString *storePath = [[NLCoreData shared] storePath]; NSError *error = nil; NSPersistentStore *persistentStore = [self.managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&amp;error]; NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error); [self.managedObjectStore createManagedObjectContexts]; self.managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:self.managedObjectStore.persistentStoreManagedObjectContext]; } - (void)setupMappings { RKObjectManager *objectManager = [RKObjectManager sharedManager]; // Mappings // banner RKEntityMapping *bannerMapping = [RKEntityMapping mappingForEntityForName:@"Banner" inManagedObjectStore:self.managedObjectStore]; [bannerMapping addAttributeMappingsFromDictionary:@{ @"title": @"title", @"id": @"bannerID", @"created_at": @"created_at", @"image": @"image", @"resource_uri": @"resource_uri", @"updated_at": @"updated_at", @"url": @"url" }]; bannerMapping.identificationAttributes = @[ @"bannerID" ]; RKResponseDescriptor *bannerDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:bannerMapping pathPattern:@"/api/v1/banner/" keyPath:@"objects" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; [objectManager addResponseDescriptor:bannerDescriptor]; // category RKEntityMapping *categoryMapping = [RKEntityMapping mappingForEntityForName:@"Category" inManagedObjectStore:self.managedObjectStore]; [categoryMapping addAttributeMappingsFromDictionary:@{ @"name": @"name", @"id": @"categoryID", @"created_at": @"created_at", @"resource_uri": @"resource_uri", @"updated_at": @"updated_at", @"active": @"active" }]; categoryMapping.identificationAttributes = @[ @"categoryID" ]; RKResponseDescriptor *categoryDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:categoryMapping pathPattern:@"/api/v1/category/" keyPath:@"objects" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; [objectManager addResponseDescriptor:categoryDescriptor]; } - (void)sendRequests { RKObjectManager *objectManager = [RKObjectManager sharedManager]; // Send Request [objectManager getObjectsAtPath:@"/api/v1/banner/" parameters:nil success:^(RKObjectRequestOperation * operation, RKMappingResult *mappingResult) { NSLog(@"SUCCESS: %@", mappingResult.array); } failure: ^(RKObjectRequestOperation * operation, NSError * error) { NSLog(@"FAILURE %@", error); }]; // category [objectManager getObjectsAtPath:@"/api/v1/category/" parameters:nil success:^(RKObjectRequestOperation * operation, RKMappingResult *mappingResult) { NSLog(@"SUCCESS: %@", mappingResult.array); } failure: ^(RKObjectRequestOperation * operation, NSError * error) { NSLog(@"FAILURE %@", error); }]; } </code></pre> <p>Any tips?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload