Last active
August 29, 2015 13:59
-
-
Save Adrian2112/10669050 to your computer and use it in GitHub Desktop.
Restkit mapping structure that worked for me (not using core data)
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
// | |
// ILAppDelegate.m | |
// | |
// Created by Adrian Gzz on 25/09/13. | |
// | |
... | |
#import "ILMappingManager.h" | |
... | |
@implementation ILAppDelegate | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
... | |
[ILMappingManager setup]; | |
... | |
} |
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
// | |
// ILMappingManager.h | |
// | |
// Created by Adrian Gonzalez on 9/18/13. | |
// | |
#import <Foundation/Foundation.h> | |
#import <RestKit/RestKit.h> | |
typedef void (^ RKSuccessBlock)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult); | |
typedef void (^ RKFailureBlock)(RKObjectRequestOperation *operation, NSError *error); | |
@interface ILMappingManager : NSObject | |
+ (void)setup; | |
+ (NSIndexSet *)statusCodeSet; | |
@end | |
@protocol ILMapper <NSObject> | |
+ (RKObjectMapping *)mapping; | |
+ (RKObjectMapping *)mappingWithRelationships; | |
+ (NSArray *)responseDescriptors; | |
+ (NSArray *)requestDescriptors; | |
+ (void)addRelationships; | |
@end |
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
// | |
// ILMappingManager.m | |
// | |
// Created by Adrian Gonzalez on 9/18/13. | |
// | |
#import "ILMappingManager.h" | |
#import "ILApiClient.h" | |
// all model mappings | |
#import "ILModelExampleMapping.h" | |
// --- | |
#import <RestKit/RestKit.h> | |
@implementation ILMappingManager | |
+ (void)setup | |
{ | |
RKObjectManager *manager = [[RKObjectManager alloc] initWithHTTPClient:[ILApiClient sharedClient]]; | |
[RKObjectManager setSharedManager:manager]; | |
NSArray *mappingClasses = @[[ILModelExampleMapping class], all model maping classes...]; | |
// preload mappings | |
[self preloadMappings:mappingClasses]; | |
// preload relationships | |
[self preloadRelatioships:mappingClasses]; | |
//Response Descriptors | |
[self addResponseDescriptorsForMappings:mappingClasses]; | |
//Request Descriptors | |
[self addRequestDescriptorsForMappings:mappingClasses]; | |
//Pagination | |
[self addPaginationMapping]; | |
} | |
+ (NSIndexSet *)statusCodeSet { | |
return RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); | |
} | |
+ (void)addResponseDescriptorsForMappings:(NSArray *)mappingClasses { | |
for (id<ILMapper> mappingClass in mappingClasses) { | |
[[RKObjectManager sharedManager] addResponseDescriptorsFromArray:[mappingClass responseDescriptors]]; | |
} | |
} | |
+ (void)addRequestDescriptorsForMappings:(NSArray *)mappingClasses { | |
for (id<ILMapper> mappingClass in mappingClasses) { | |
[[RKObjectManager sharedManager] addRequestDescriptorsFromArray:[mappingClass requestDescriptors]]; | |
} | |
} | |
+ (void)addPaginationMapping | |
{ | |
RKObjectMapping *paginationMapping = [RKObjectMapping mappingForClass:[RKPaginator class]]; | |
RKObjectManager *objectManager = [RKObjectManager sharedManager]; | |
[paginationMapping addAttributeMappingsFromDictionary:@{ | |
@"meta.pagination.per_page":@"perPage", | |
@"meta.pagination.total_pages":@"pageCount", | |
@"meta.pagination.total_objects":@"objectCount" | |
}]; | |
[objectManager setPaginationMapping:paginationMapping]; | |
} | |
+(void)preloadMappings:(NSArray *)mappingClasses | |
{ | |
for (id<ILMapper> mappingClass in mappingClasses) { | |
[mappingClass mapping]; | |
} | |
} | |
+(void)preloadRelatioships:(NSArray *)mappingClasses | |
{ | |
for (id<ILMapper> mappingClass in mappingClasses) { | |
[mappingClass addRelationships]; | |
} | |
} | |
@end |
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
// | |
// ILModelMappingExample.h | |
// | |
// Created by Adrian Gzz on 17/10/13. | |
// | |
#import <Foundation/Foundation.h> | |
#import <RestKit/RestKit.h> | |
#import "ILMappingManager.h" | |
@interface ILModelMappingExample : NSObject <ILMapper> | |
+ (NSArray *) responseDescriptors; | |
+ (NSArray *) requestDescriptors; | |
+ (RKObjectMapping *) mapping; | |
+ (RKObjectMapping *)mappingWithRelationships; | |
+ (void)addRelationships; | |
@end |
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
// | |
// ILModelMappingExample.m | |
// | |
// Created by Adrian Gonzalez on 9/18/13. | |
// | |
#import "ILModelMappingExample.h" | |
#import "ILModelExample.h" | |
#import "ILMappingManager.h" | |
#import <RestKit/RestKit.h> | |
@implementation ILModelMappingExample | |
+ (RKObjectMapping *)mapping | |
{ | |
static RKObjectMapping *_mapping = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[ILModelExample class]]; | |
[mapping addAttributeMappingsFromArray:@[@"name", @"email", ...]]; | |
[mapping addAttributeMappingsFromDictionary:@{ | |
@"id": @"userId", | |
... | |
}]; | |
_mapping = mapping; | |
}); | |
return _mapping; | |
} | |
+(RKObjectMapping *)mappingWithRelationships | |
{ | |
static RKObjectMapping *_mapping = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
_mapping = [[self mapping] copy]; | |
}); | |
return _mapping; | |
} | |
+(void)addRelationships | |
{ | |
RKObjectMapping *mapping = [self mappingWithRelationships]; | |
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"friends" | |
toKeyPath:@"friends" | |
withMapping:[ILModelMappingExample mappingWithRelationships]]]; | |
} | |
+ (NSArray *)responseDescriptors | |
{ | |
RKResponseDescriptor *showDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[self mappingWithRelationships] | |
method:RKRequestMethodGET | |
pathPattern:@"model_example/:id" | |
keyPath:@"model_example" | |
statusCodes:[ILModelMappingExample statusCodeSet]]; | |
RKResponseDescriptor *otherDescriptor = ... | |
return @[showDescriptor, otherDescriptor, ...]; | |
} | |
+ (NSArray *)requestDescriptors | |
{ | |
RKRequestDescriptor *createDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[[self mapping] inverseMapping] | |
objectClass:[ILModelExample class] | |
rootKeyPath:@"model_example" | |
method:RKRequestMethodAny]; | |
RKRequestDescriptor *otherDescriptor = ... | |
return @[createDescriptor, otherDescriptor, ...]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment