Add a new AGPagedList
category on the NSArray
class:
@interface NSArray (AGPagedList)
/**
* Reads the first 'page' of the paging result, from the server.
*
* @param success A block object to be executed when the request operation
* finishes successfully. This block has no return value and takes one
* argument: The object created from the response data of request.
*
* @param failure A block object to be executed when the request operation
* finishes unsuccessfully, or that finishes successfully, but encountered
* an error while parsing the resonse data. This block has no return value
* and takes one argument: The `NSError` object describing the network or
* parsing error that occurred.
*/
-(void) first:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
/**
* Reads the last 'page' of the paging result, from the server.
*
* @param success A block object to be executed when the request operation
* finishes successfully. This block has no return value and takes one
* argument: The object created from the response data of request.
*
* @param failure A block object to be executed when the request operation
* finishes unsuccessfully, or that finishes successfully, but encountered
* an error while parsing the resonse data. This block has no return value
* and takes one argument: The `NSError` object describing the network or
* parsing error that occurred.
*/
-(void) last:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
/**
* Reads the next 'page', based on the current position, of the paging result, from the server.
*
* @param success A block object to be executed when the request operation
* finishes successfully. This block has no return value and takes one
* argument: The object created from the response data of request.
*
* @param failure A block object to be executed when the request operation
* finishes unsuccessfully, or that finishes successfully, but encountered
* an error while parsing the resonse data. This block has no return value
* and takes one argument: The `NSError` object describing the network or
* parsing error that occurred.
*/
-(void) next:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
/**
* Reads the previous 'page', based on the current position, of the paging result, from the server.
*
* @param success A block object to be executed when the request operation
* finishes successfully. This block has no return value and takes one
* argument: The object created from the response data of request.
*
* @param failure A block object to be executed when the request operation
* finishes unsuccessfully, or that finishes successfully, but encountered
* an error while parsing the resonse data. This block has no return value
* and takes one argument: The `NSError` object describing the network or
* parsing error that occurred.
*/
-(void) previous:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
/**
* Returns YES if the we can go to the first 'page' of the paging result.
*/
-(BOOL) hasFirst;
/**
* Returns YES if the we can go to the last 'page' of the paging result.
*/
-(BOOL) hasLast;
/**
* Returns YES if the we can go to the next 'page' of the paging result.
*/
-(BOOL) hasNext;
/**
* Returns YES if the we can go to the previous 'page' of the paging result.
*/
-(BOOL) hasPrevious;
@end
An object from that class will be returned on the success
block, when doing a [pipe readWithFilter:success:failure];
.
We also need to enhance the existing AGFilterConfig
@protocol AGFilterConfig <NSObject>
/**
* Applies where the library should look for the 'scrolling' metadata.
* Scrolling metadata can be found on the header (default) or on the
* response body (content), like the twitter search does.
*/
@property (assign, nonatomic) NSString *scrollingMetaDataLocation; //header||content
/**
* Applies the actual limit to the configuration.
*/
@property (assign, nonatomic) NSUInteger limit;
/**
* Applies the offset to the configuration.
*/
@property (assign, nonatomic) NSUInteger offset;
/**
* Applies the 'where' query to the configuration.
*/
@property (strong, nonatomic) NSDictionary* where;
// TODO:
// * define params to override the http query params
// * define params to override the default header names (e.g. AG-Paging-Offset)
// * define a way to ignore the Web Linking spec, in cases like Twitter, where they
// render some bogus metadata inside of the response body...
//
@end
No need to change the readWithFilter
from the AGPipe
, which starts of the paging...
-(void) readWithFilter:(void (^)(id<AGFilterConfig> config)) config
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
How to use (including "redefining" the pagination)?
AGPipeline *testPipeline = [AGPipeline pipelineWithBaseURL:_baseURL];
id<AGPipe> pipe = [testPipeline pipe:^(id<AGPipeConfig> config) {
[config setName:@"cars"];
}];
// the AGPagedList category:
__block NSArray *_pagedList;
// start the paging........
[pipe readWithFilter:^(id<AGFilterConfig> config) {
//set up the paging details:
[config setLimit:3];
[config setOffset:0];
} success:^(id responseObject) {
// stash the "paged/query result" object (or what ever the name will be)
_pagedList = responseObject;
// show the results - somehwre
NSLog(@"RESULT, PAGE 1: %@", listOfObjects);
} failure:^(NSError *error) {
}];
// get the second page.....
[_pagedList next:^(id responseObject) {
// show the results - somehwre
NSLog(@"RESULT, PAGE 2: %@", responseObject);
} failure:^(NSError *error) {
}];
// go backwards....
[_pagedList previous:^(id responseObject) {
/// do something
} failure:^(NSError *error) {
}];
// REDEFINE the query/pagination:
[pipe readWithFilter:^(id<AGFilterConfig> config) {
// now we want 10 per page...., starting at page 1.........
[config setLimit:10];
[config setOffset:1];
} success:^(id responseObject) {
// stash the "paged/query result" object (or what ever the name will be)
_pagedList = responseObject;
// show the results - somehwre
NSLog(@"RESULT, PAGE 1: %@", listOfObjects);
} failure:^(NSError *error) {
}];
// get more data.......
[_pagedList next:^(id responseObject) {
// show the results - somehwre
NSLog(@"RESULT, PAGE 2: %@", responseObject);
} failure:^(NSError *error) {
}];