RKPaginator Class Reference
| Inherits from | NSObject |
| Declared in | RKPaginator.h |
Overview
Instances of RKPaginator retrieve paginated collections of mappable data from remote systems via HTTP. Paginators perform GET requests and use a patterned URL to construct a full URL reflecting the state of the paginator. Paginators rely on an instance of RKObjectMappingProvider to determine how to perform object mapping on the retrieved data. Paginators can load Core Data backed models provided that an instance of RKManagedObjectStore is assigned to the paginator.
Configuring Pagination Mapping
The paginator must be configured with a paginationMapping specifying how configuration metadata is to be mapped out of the response payload. The configured mapping must have an objectClass of RKPaginator and should include attribute mappings for the currentPage, pageCount, perPage, and objectCount. For example, given a paginated resource loaded from ‘/articles?page=1’ with the followibg JSON:
{ "pagination": { "per_page": 10, "total_pages": 25, "total_objects": 250 }, "articles": [ // Array of articles ] }
The pagination mapping would be configured as:
RKObjectMapping *paginationMapping = [RKObjectMapping mappingForClass:[RKPaginator class]];
[paginationMapping addAttributeMappingsFromDictionary:@{
@"pagination.per_page": @"perPage",
@"pagination.total_pages": @"pageCount",
@"pagination.total_objects": @"objectCount",
}];
iOS 5 Compatibility Caveats
The paginator is compatible with iOS 5.x through the use of proxy attributes. In iOS 6.0 and greater, key-value coding supports the automatic boxing and unboxing of primitive values. This enables direct mapping configuration for the currentPage, pageCount, perPage, and objectCount attributes. Under iOS 5, where autoboxing is not available, mapping configuration must target special proxy attributes instead. For each of the above properties, a private NSNumber property is implemented by the class. Each proxy property has ‘Number’ appended as a suffix to the property name: currentPageNumber, pageCountNumber, perPageNumber, and objectCountNumber.
Tasks
Initializing Paginator Objects
-
– initWithRequest:paginationMapping:responseDescriptors:Initializes a RKPaginator object with the a provided patternURL and mappingProvider.
Configuring Networking
-
patternURLA URL with a path pattern for building a complete URL from which to load the paginated resource collection. The patterned resource path will be evaluated against the state of the paginator object itself.
property -
URLReturns a complete URL to the paginated resource collection by interpolating the state of the paginator object against the patternURL.
property -
operationQueueAn optional operation queue on which object request operations constructed by the paginator are to be enqueued for processing.
property -
– setHTTPOperationClass:Sets the
RKHTTPRequestOperationsubclass to be used when constructing HTTP request operations for requests dispatched by the paginator.
Setting the Completion Block
-
– setCompletionBlockWithSuccess:failure:Sets the completion block to be invoked when the paginator finishes loading a page of results.
Accessing Pagination Results
-
mappingResultThe mapping result containing the last set of paginated objects or
propertynilif an error was encountered. -
errorThe error, if any, that occured during the last load of the paginator.
property
Object Mapping Configuration
-
paginationMappingThe object mapping defining how pagination metadata is to be mapped from a paginated response onto the paginator object.
property
Core Data Configuration
-
managedObjectContextThe managed object context in which paginated managed objects are to be persisted.
property -
managedObjectCacheThe managed object cache used to find existing managed object instances in the persistent store.
property -
fetchRequestBlocksAn array of fetch request blocks.
property
Accessing Pagination Metadata
-
perPageThe number of objects to load per page
property -
loadedA Boolean value indicating if the paginator has loaded a page of objects
property -
currentPageReturns the page number for the most recently loaded page of objects.
property -
offsetReturns the offset based off the page for the most recently loaded objects.
property -
pageCountReturns the number of pages in the total resource collection.
property -
objectCountReturns the total number of objects in the collection
property -
– hasPageCountReturns a Boolean value indicating if the total number of pages in the collection is known by the paginator.
-
– hasObjectCountReturns a Boolean value indicating if the total number of objects in the collection is known by the paginator.
-
– hasNextPageReturns a Boolean value indicating if there is a next page in the collection.
-
– hasPreviousPageReturns a Boolean value indicating if there is a previous page in the collection.
Paginator Actions
-
– loadNextPageLoads the next page of data by incrementing the current page, constructing an object loader to fetch the data, and object mapping the results.
-
– loadPreviousPageLoads the previous page of data by decrementing the current page, constructing an object loader to fetch the data, and object mapping the results.
-
– loadPage:Loads a specific page of data by mutating the current page, constructing an object loader to fetch the data, and object mapping the results.
-
– cancelCancels an in-progress pagination request.
Properties
URL
Returns a complete URL to the paginated resource collection by interpolating the state of the paginator object against the patternURL.
@property (nonatomic, readonly) NSURL *URLDeclared In
RKPaginator.hcurrentPage
Returns the page number for the most recently loaded page of objects.
@property (nonatomic, readonly) NSUInteger currentPageReturn Value
The page number for the current page of objects.
Exceptions
- NSInternalInconsistencyException
Raised if
isLoadedis equal toNO.
Declared In
RKPaginator.herror
The error, if any, that occured during the last load of the paginator.
@property (nonatomic, strong, readonly) NSError *errorDeclared In
RKPaginator.hfetchRequestBlocks
An array of fetch request blocks.
@property (nonatomic, copy) NSArray *fetchRequestBlocksDeclared In
RKPaginator.hloaded
A Boolean value indicating if the paginator has loaded a page of objects
@property (nonatomic, readonly, getter=isLoaded) BOOL loadedReturn Value
YES when the paginator has loaded a page of objects
Declared In
RKPaginator.hmanagedObjectCache
The managed object cache used to find existing managed object instances in the persistent store.
@property (nonatomic, strong) id<RKManagedObjectCaching> managedObjectCacheDeclared In
RKPaginator.hmanagedObjectContext
The managed object context in which paginated managed objects are to be persisted.
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContextDeclared In
RKPaginator.hmappingResult
The mapping result containing the last set of paginated objects or nil if an error was encountered.
@property (nonatomic, strong, readonly) RKMappingResult *mappingResultDeclared In
RKPaginator.hobjectCount
Returns the total number of objects in the collection
@property (nonatomic, readonly) NSUInteger objectCountReturn Value
A count of the number of objects in the resource collection.
Exceptions
- NSInternalInconsistencyException
Raised if
hasObjectCountisNO.
Declared In
RKPaginator.hoffset
Returns the offset based off the page for the most recently loaded objects.
@property (nonatomic, readonly) NSUInteger offsetReturn Value
The offset for the current page of objects.
Exceptions
- NSInternalInconsistencyException
Raised if
isLoadedis equal toNO.
Declared In
RKPaginator.hoperationQueue
An optional operation queue on which object request operations constructed by the paginator are to be enqueued for processing.
@property (nonatomic, strong) NSOperationQueue *operationQueueDeclared In
RKPaginator.hpageCount
Returns the number of pages in the total resource collection.
@property (nonatomic, readonly) NSUInteger pageCountReturn Value
A count of the number of pages in the resource collection.
Exceptions
- NSInternalInconsistencyException
Raised if
hasPageCountisNO.
Declared In
RKPaginator.hpaginationMapping
The object mapping defining how pagination metadata is to be mapped from a paginated response onto the paginator object.
@property (nonatomic, strong) RKObjectMapping *paginationMappingDiscussion
See the documentation in the “Configuring Pagination Mapping” section for details about the pagination mapping.
Warning: The objectClass of the given mapping must be RKPaginator.
Declared In
RKPaginator.hpatternURL
A URL with a path pattern for building a complete URL from which to load the paginated resource collection. The patterned resource path will be evaluated against the state of the paginator object itself.
@property (nonatomic, readonly) NSURL *patternURLDiscussion
For example, given a paginated collection of data at the /articles path, the path portion of the pattern URL may look like:
/articles?per_page=:perPage&page_number=:currentPage
When the pattern is evaluated against the state of the paginator, this will yield a complete path that can be used to load the specified page. Given a paginator configured with 100 objects per page and a current page number of 3, the path portion of the pagination URL would become:
/articles?per_page=100&page_number=3
Declared In
RKPaginator.hInstance Methods
hasNextPage
Returns a Boolean value indicating if there is a next page in the collection.
- (BOOL)hasNextPageReturn Value
YES if there is a next page, otherwise NO.
Exceptions
- NSInternalInconsistencyException
Raised if isLoaded or hasPageCount is
NO.
Declared In
RKPaginator.hhasObjectCount
Returns a Boolean value indicating if the total number of objects in the collection is known by the paginator.
- (BOOL)hasObjectCountReturn Value
YES if the paginator knows the number of objects in the paginated collection, otherwise NO.
Declared In
RKPaginator.hhasPageCount
Returns a Boolean value indicating if the total number of pages in the collection is known by the paginator.
- (BOOL)hasPageCountReturn Value
YES if the paginator knows the page count, otherwise NO.
Declared In
RKPaginator.hhasPreviousPage
Returns a Boolean value indicating if there is a previous page in the collection.
- (BOOL)hasPreviousPageReturn Value
YES if there is a previous page, otherwise NO.
Exceptions
- NSInternalInconsistencyException
Raised if isLoaded is
NO.
Declared In
RKPaginator.hinitWithRequest:paginationMapping:responseDescriptors:
Initializes a RKPaginator object with the a provided patternURL and mappingProvider.
- (id)initWithRequest:(NSURLRequest *)request paginationMapping:(RKObjectMapping *)paginationMapping responseDescriptors:(NSArray *)responseDescriptorsParameters
- request
A request with a URL containing a dynamic pattern specifying how paginated resources are to be acessed.
- paginationMapping
The pagination mapping specifying how pagination metadata is to be mapped from responses.
- responseDescriptors
An array of response descriptors describing how to map object representations loaded by object request operations dispatched by the paginator.
Return Value
The receiver, initialized with the request, pagination mapping, and response descriptors.
Declared In
RKPaginator.hloadNextPage
Loads the next page of data by incrementing the current page, constructing an object loader to fetch the data, and object mapping the results.
- (void)loadNextPageDeclared In
RKPaginator.hloadPage:
Loads a specific page of data by mutating the current page, constructing an object loader to fetch the data, and object mapping the results.
- (void)loadPage:(NSUInteger)pageNumberParameters
- pageNumber
The page of objects to load from the remote backend
Declared In
RKPaginator.hloadPreviousPage
Loads the previous page of data by decrementing the current page, constructing an object loader to fetch the data, and object mapping the results.
- (void)loadPreviousPageDeclared In
RKPaginator.hsetCompletionBlockWithSuccess:failure:
Sets the completion block to be invoked when the paginator finishes loading a page of results.
- (void)setCompletionBlockWithSuccess:(void ( ^ ) ( RKPaginator *paginator , NSArray *objects , NSUInteger page ))success failure:(void ( ^ ) ( RKPaginator *paginator , NSError *error ))failureParameters
- success
A block to be executed upon a successful load of a page of objects. The block has no return value and takes three arguments: the paginator object, an array containing the paginated objects, and an integer indicating the page that was loaded.
- failure
A block to be exected upon a failed load. The block has no return value and takes two arguments: the paginator object and an error indicating the nature of the failure.
Declared In
RKPaginator.hsetHTTPOperationClass:
Sets the RKHTTPRequestOperation subclass to be used when constructing HTTP request operations for requests dispatched by the paginator.
- (void)setHTTPOperationClass:(Class)operationClassDiscussion
Default: [RKHTTPRequestOperation class]
Declared In
RKPaginator.h