Created
April 4, 2012 00:31
-
-
Save bobspryn/2296677 to your computer and use it in GitHub Desktop.
DataLoader.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Configuration elsewhere in the file | |
| self.paginationMapping = [RKObjectMapping mappingForClass:[RKObjectPaginator class]]; | |
| [paginationMapping mapKeyPath:@"current_page" toAttribute:@"currentPage"]; | |
| [paginationMapping mapKeyPath:@"per_page" toAttribute:@"perPage"]; | |
| [paginationMapping mapKeyPath:@"total_entries" toAttribute:@"objectCount"]; | |
| // Object Mappings for Unit Pricing | |
| self.unitPricingMapping = [RKManagedObjectMapping mappingForClass:[UnitPricingEntry class] inManagedObjectStore:self.objectManager.objectStore]; | |
| [unitPricingMapping mapKeyPath:@"id" toAttribute:@"unitPricingEntryId"]; | |
| [unitPricingMapping setPrimaryKeyAttribute:@"unitPricingEntryId"]; | |
| [unitPricingMapping mapAttributes:@"unitId", @"leaseTermId", @"rent", @"startDate", @"endDate", @"active", nil]; | |
| [unitPricingMapping mapRelationship:@"leaseTerm" withMapping:leaseTermMapping]; | |
| [unitPricingMapping connectRelationship:@"leaseTerm" withObjectForPrimaryKeyAttribute:@"leaseTermId"]; | |
| [unitPricingMapping mapRelationship:@"unit" withMapping:unitMapping]; | |
| [unitPricingMapping connectRelationship:@"unit" withObjectForPrimaryKeyAttribute:@"unitId"]; | |
| self.objectManager.mappingProvider.paginationMapping = paginationMapping; | |
| [self.objectManager.mappingProvider setMapping:unitPricingMapping forKeyPath:@"entries"]; | |
| // this is executed after many loadObjectsAtResourcePath are called, so should be queued up behind them | |
| NSString *unitPricingUrl = [self createCommuntiyApiUrl:@"unit-variable-rent?page=:currentPage&totalPerPage=:perPage" sinceDate:self.lastUnitPricingSync]; | |
| RKURL *url = [RKURL URLWithBaseURL:self.objectManager.baseURL resourcePath:unitPricingUrl]; | |
| RKObjectPaginator *paginator = [RKObjectPaginator paginatorWithPatternURL:url mappingProvider:self.objectManager.mappingProvider]; | |
| paginator.delegate = self; | |
| [paginator loadPage:1]; | |
| paginator.onDidLoadObjectsForPage = ^(NSArray *objects, NSUInteger page) { | |
| if (paginator.hasNextPage) { | |
| [paginator loadNextPage]; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment