Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added block callback handlers and tests for RKObjectPaginator.

  • Loading branch information...
commit d69adccdd6a6c632ff7c92fb602170726cdc29f5 1 parent 1d27f7b
@blakewatters blakewatters authored
View
49 Code/ObjectMapping/RKObjectPaginator.h
@@ -25,6 +25,9 @@
@protocol RKObjectPaginatorDelegate;
+typedef void(^RKObjectPaginatorDidLoadObjectsForPageBlock)(NSArray *objects, NSUInteger page);
+typedef void(^RKObjectPaginatorDidFailWithErrorBlock)(NSError *error, RKObjectLoader *loader);
+
/**
Instances of RKObjectPaginator retrieve paginated collections of mappable data
from remote systems via HTTP. Paginators perform GET requests and use a patterned
@@ -92,6 +95,20 @@
@property (nonatomic, assign) id<RKObjectPaginatorDelegate> delegate;
/**
+ The block to invoke when the paginator has loaded a page of objects from the collection.
+
+ @see [RKObjectPaginatorDelegate paginator:didLoadObjects:forPage]
+ */
+@property (nonatomic, copy) RKObjectPaginatorDidLoadObjectsForPageBlock onDidLoadObjectsForPage;
+
+/**
+ The block to invoke when the paginator has failed loading due to an error.
+
+ @see [RKObjectPaginatorDelegate paginator:didFailWithError:objectLoader:]
+ */
+@property (nonatomic, copy) RKObjectPaginatorDidFailWithErrorBlock onDidFailWithError;
+
+/**
The object that acts as the configuration delegate for RKObjectLoader instances built
and utilized by the paginator.
@@ -213,45 +230,45 @@
@protocol RKObjectPaginatorDelegate <NSObject>
/**
- Sent to the delegate when the paginator has loaded a collection of objects for a given page
+ Tells the delegate the paginator has loaded a page of objects from the collection.
- @param paginator The paginator that loaded the objects
- @param objects An array of objects mapped from the remote JSON/XML representation
- @param page The page number that was loaded
+ @param paginator The paginator that loaded the objects.
+ @param objects An array of objects mapped from the remote JSON/XML representation.
+ @param page The page number that was loaded.
*/
- (void)paginator:(RKObjectPaginator *)paginator didLoadObjects:(NSArray *)objects forPage:(NSUInteger)page;
/**
- Sent to the delegate when the paginator has failed loading due to an error
+ Tells the delegate the paginator has failed loading due to an error.
- @param paginator The paginator that failed loading due to an error
- @param error An NSError indicating the cause of the failure
- @param loader The loader request that resulted in the failure
+ @param paginator The paginator that failed loading due to an error.
+ @param error An NSError indicating the cause of the failure.
+ @param loader The loader request that resulted in the failure.
*/
- (void)paginator:(RKObjectPaginator *)paginator didFailWithError:(NSError *)error objectLoader:(RKObjectLoader *)loader;
@optional
/**
- Sent to the delegate before the paginator begins loading a page
+ Tells the delegate that the paginator is about to begin loading a page of objects.
- @param paginator The paginator performing the load
- @param page The numeric page number being loaded
- @param loader The object loader request used to load the page
+ @param paginator The paginator performing the load.
+ @param page The numeric page number being loaded.
+ @param loader The object loader request used to load the page.
*/
- (void)paginator:(RKObjectPaginator *)paginator willLoadPage:(NSUInteger)page objectLoader:(RKObjectLoader *)loader;
/**
- Sent to the delegate when the paginator has loaded the first page in the collection
+ Tells the delegate the paginator has loaded the first page of objects in the collection.
- @param paginator The paginator instance that has loaded the first page
+ @param paginator The paginator instance that has loaded the first page.
*/
- (void)paginatorDidLoadFirstPage:(RKObjectPaginator *)paginator;
/**
- Sent to the delegate when the paginator has loaded the last page in the collection
+ Tells the delegate the paginator has loaded the last page of objects in the collection.
- @param paginator The paginator instance that has loaded the last page
+ @param paginator The paginator instance that has loaded the last page.
*/
- (void)paginatorDidLoadLastPage:(RKObjectPaginator *)paginator;
View
19 Code/ObjectMapping/RKObjectPaginator.m
@@ -44,6 +44,8 @@ @implementation RKObjectPaginator
@synthesize objectStore;
@synthesize objectLoader;
@synthesize configurationDelegate;
+@synthesize onDidLoadObjectsForPage;
+@synthesize onDidFailWithError;
+ (id)paginatorWithPatternURL:(RKURL *)aPatternURL mappingProvider:(RKObjectMappingProvider *)aMappingProvider {
return [[[self alloc] initWithPatternURL:aPatternURL mappingProvider:aMappingProvider] autorelease];
@@ -78,6 +80,10 @@ - (void)dealloc {
objectLoader.delegate = nil;
[objectLoader release];
objectLoader = nil;
+ [onDidLoadObjectsForPage release];
+ onDidLoadObjectsForPage = nil;
+ [onDidFailWithError release];
+ onDidFailWithError = nil;
[super dealloc];
}
@@ -134,6 +140,10 @@ - (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)ob
RKLogInfo(@"Loaded objects: %@", objects);
[self.delegate paginator:self didLoadObjects:objects forPage:self.currentPage];
+ if (self.onDidLoadObjectsForPage) {
+ self.onDidLoadObjectsForPage(objects, self.currentPage);
+ }
+
if ([self hasPageCount] && self.currentPage == 1) {
if ([self.delegate respondsToSelector:@selector(paginatorDidLoadFirstPage:)]) {
[self.delegate paginatorDidLoadFirstPage:self];
@@ -148,9 +158,12 @@ - (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)ob
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
- RKLogError(@"Paginator error %@", error);
- [self.delegate paginator:self didFailWithError:error objectLoader:self.objectLoader];
- self.objectLoader = nil;
+ RKLogError(@"Paginator error %@", error);
+ [self.delegate paginator:self didFailWithError:error objectLoader:self.objectLoader];
+ if (self.onDidFailWithError) {
+ self.onDidFailWithError(error, self.objectLoader);
+ }
+ self.objectLoader = nil;
}
- (void)objectLoader:(RKObjectLoader *)loader willMapData:(inout id *)mappableData {
View
30 Specs/ObjectMapping/RKObjectPaginatorSpec.m
@@ -285,6 +285,21 @@ - (void)testLoadingPageOfObjectHasObjectCount {
assertThatBool([paginator hasObjectCount], is(equalToBool(YES)));
}
+- (void)testOnDidLoadObjectsForPageBlockIsInvokedOnLoad {
+ RKURL *patternURL = [RKSpecGetBaseURL() URLByAppendingResourcePath:RKObjectPaginatorSpecResourcePathPattern];
+ RKObjectMappingProvider *mappingProvider = [self paginationMappingProvider];
+ RKObjectPaginator *paginator = [RKObjectPaginator paginatorWithPatternURL:patternURL mappingProvider:mappingProvider];
+ RKSpecPaginatorDelegate *testDelegate = [RKSpecPaginatorDelegate paginatorDelegate];
+ paginator.delegate = testDelegate;
+ __block NSArray *blockObjects = nil;
+ paginator.onDidLoadObjectsForPage = ^(NSArray *objects, NSUInteger page) {
+ blockObjects = objects;
+ };
+ [paginator loadPage:1];
+ [testDelegate waitForLoad];
+ assertThat(blockObjects, is(notNilValue()));
+}
+
- (void)testDelegateIsInformedOfWillLoadPage {
RKURL *patternURL = [RKSpecGetBaseURL() URLByAppendingResourcePath:RKObjectPaginatorSpecResourcePathPattern];
RKObjectMappingProvider *mappingProvider = [self paginationMappingProvider];
@@ -311,6 +326,21 @@ - (void)testDelegateIsInformedOnError {
[mockDelegate verify];
}
+- (void)testOnDidFailWithErrorBlockIsInvokedOnError {
+ RKURL *patternURL = [RKSpecGetBaseURL() URLByAppendingResourcePath:RKObjectPaginatorSpecResourcePathPattern];
+ RKObjectMappingProvider *mappingProvider = [self paginationMappingProvider];
+ RKObjectPaginator *paginator = [RKObjectPaginator paginatorWithPatternURL:patternURL mappingProvider:mappingProvider];
+ __block NSError *expectedError = nil;
+ paginator.onDidFailWithError = ^(NSError *error, RKObjectLoader *loader) {
+ expectedError = error;
+ };
+ RKSpecPaginatorDelegate *testDelegate = [RKSpecPaginatorDelegate paginatorDelegate];
+ paginator.delegate = testDelegate;
+ [paginator loadPage:999];
+ [testDelegate waitForLoad];
+ assertThat(expectedError, is(notNilValue()));
+}
+
- (void)testDelegateIsInformedOnLoadOfFirstPage {
RKURL *patternURL = [RKSpecGetBaseURL() URLByAppendingResourcePath:RKObjectPaginatorSpecResourcePathPattern];
RKObjectMappingProvider *mappingProvider = [self paginationMappingProvider];
Please sign in to comment.
Something went wrong with that request. Please try again.