Skip to content

Loading…

* Implement paginator construction relative to a path resolved from an object #1136

Closed
wants to merge 1 commit into from

5 participants

@pcuenca

This facilitates pagination URLs to be derived from parametrized URL routes. Consider, for example, the following path pattern: @"users/:userId/articles"

Assuming that the response can be paginated when the "page" query string parameter is supplied, the following will create a paginator for the articles of a given user:

RKPaginator *articlePaginator = [objectManager paginatorForObject:user path:nil parameters:nil withRelativePathPattern:@"?page=:currentPage"];
[articlePaginator setCompletionBlockWithSuccess:^(RKPaginator *paginator, NSArray *objects, NSUInteger page) {
    /* success block */
} failure:^(RKPaginator *paginator, NSError *error) {
    /* failure block */
}];
[articlePaginator loadPage: 0];

I wonder if there is a simpler way to achieve the same using the paginator code in the base repo. Thanks!

@pcuenca pcuenca * Implement paginator construction relative to a path resolved from a…
…n object.

This facilitates pagination URLs to be derived from parametrized URL routes. Consider, for example, the following path pattern:
@"users/:userId/articles"

Assuming that the response can be paginated when the "page" query string parameter is supplied, the following will create a paginator for the articles of a given user:

RKPaginator * articlePaginator = [objectManager paginatorForObject: user path: nil parameters: nil withRelativePathPattern: @"?page=:currentPage"];
[articlePaginator setCompletionBlockWithSuccess:^(RKPaginator *paginator, NSArray *objects, NSUInteger page) {
    /* success block */
} failure:^(RKPaginator *paginator, NSError *error) {
    /* failure block */
}];
[articlePaginator loadPage: 0];
9c432d2
@blakewatters blakewatters was assigned
@blakewatters
The RestKit Project member

Interesting. Will consider this for inclusion. I am somewhat resistant to adding methods to RKObjectManager, but will give it consideration.

@pcuenca

I understand that. However, I did not find a better place to handle this - paginators were already being configured in RKObjectManager, I'm guessing because they need access to the response descriptors and the CoreData context. I also considered the approach to automatically configure pagination for a certain set of path patterns, but that would be more difficult to tackle and I don't know if it's worth the effort. If that were to be undertaken, I presume the object manager would be the entity in charge.

In any case, I would have thought paginating parametrized URLs would be a relatively common task, any ideas on how other users are tackling this case?

@ipmcc

Like @pcuenca, I would also like to paginate parameterized URLs. In general, I find the whole RKPaginator construction scenario to be needlessly opaque. There are options on RKPaginator that are set in paginatorWithPathPattern: that require access to non-public methods of the RKObjectManager, but the request is not mutable after initialization. This seems... suboptimal.

@blakewatters
The RestKit Project member

Bumped to 0.20.1 as I am trying to hold the line on bugfixes to get 0.20.0 out the door.

@Kurry
The RestKit Project member

@blakewatters Whats the status on this? I can change it or modify it if needed.

@segiddins segiddins closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 8, 2013
  1. @pcuenca

    * Implement paginator construction relative to a path resolved from a…

    pcuenca committed
    …n object.
    
    This facilitates pagination URLs to be derived from parametrized URL routes. Consider, for example, the following path pattern:
    @"users/:userId/articles"
    
    Assuming that the response can be paginated when the "page" query string parameter is supplied, the following will create a paginator for the articles of a given user:
    
    RKPaginator * articlePaginator = [objectManager paginatorForObject: user path: nil parameters: nil withRelativePathPattern: @"?page=:currentPage"];
    [articlePaginator setCompletionBlockWithSuccess:^(RKPaginator *paginator, NSArray *objects, NSUInteger page) {
        /* success block */
    } failure:^(RKPaginator *paginator, NSError *error) {
        /* failure block */
    }];
    [articlePaginator loadPage: 0];
Showing with 25 additions and 2 deletions.
  1. +5 −0 Code/Network/RKObjectManager.h
  2. +20 −2 Code/Network/RKObjectManager.m
View
5 Code/Network/RKObjectManager.h
@@ -829,6 +829,11 @@ RKMappingResult, RKRequestDescriptor, RKResponseDescriptor;
*/
- (RKPaginator *)paginatorWithPathPattern:(NSString *)pathPattern;
+- (RKPaginator *)paginatorForObject:(id)object
+ path:(NSString *)path
+ parameters:(NSDictionary *)parameters
+ withRelativePathPattern:(NSString *)paginatorPathPattern;
+
@end
#ifdef _SYSTEMCONFIGURATION_H
View
22 Code/Network/RKObjectManager.m
@@ -606,10 +606,9 @@ - (void)deleteObject:(id)object
[self enqueueObjectRequestOperation:operation];
}
-- (RKPaginator *)paginatorWithPathPattern:(NSString *)pathPattern
+- (RKPaginator *)paginatorForRequest:(NSURLRequest *)request
{
NSAssert(self.paginationMapping, @"Cannot instantiate a paginator when `paginationMapping` is nil.");
- NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:pathPattern parameters:nil];
RKPaginator *paginator = [[RKPaginator alloc] initWithRequest:request paginationMapping:self.paginationMapping responseDescriptors:self.responseDescriptors];
paginator.managedObjectContext = self.managedObjectStore.mainQueueManagedObjectContext;
paginator.managedObjectCache = self.managedObjectStore.managedObjectCache;
@@ -619,6 +618,25 @@ - (RKPaginator *)paginatorWithPathPattern:(NSString *)pathPattern
return paginator;
}
+- (RKPaginator *)paginatorWithPathPattern:(NSString *)pathPattern
+{
+ NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:pathPattern parameters:nil];
+ return [self paginatorForRequest:request];
+}
+
+- (RKPaginator *)paginatorForObject:(id)object
+ path:(NSString *)path
+ parameters:(NSDictionary *)parameters
+ withRelativePathPattern:(NSString *)paginatorPathPattern
+{
+ RKRequestMethod method = RKRequestMethodGET;
+ NSString *requestPath = (path) ? path : [[self.router URLForObject:object method:method] relativeString];
+ requestPath = [requestPath stringByAppendingPathComponent: paginatorPathPattern];
+ id requestParameters = [self mergedParametersWithObject:object method:method parameters:parameters];
+ NSMutableURLRequest *request = [self requestWithMethod:RKStringFromRequestMethod(method) path:requestPath parameters:requestParameters];
+ return [self paginatorForRequest: request];
+}
+
#pragma mark - Request & Response Descriptors
- (NSArray *)requestDescriptors
Something went wrong with that request. Please try again.