Last active
August 29, 2015 14:10
-
-
Save alexvbush/65ae66cd0e43c9845b7f to your computer and use it in GitHub Desktop.
RestKit RKObjectManager Setup
This file contains 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
// | |
// HLPlaceResourceManager.h | |
// HeyLets2 | |
// | |
// Created by Alex Bush on 11/18/14. | |
// Copyright (c) 2014 HeyLets. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@class CDPlace; | |
@interface HLPlaceResourceManager : NSObject | |
+ (id)sharedInstance; | |
- (void)fetchPlaceWithID:(NSNumber *)placeID | |
success:(void (^)(CDPlace *place))success | |
failure:(void (^)(NSError *error))failure; | |
@end |
This file contains 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
// | |
// HLPlaceResourceManager.m | |
// HeyLets2 | |
// | |
// Created by Alex Bush on 11/18/14. | |
// Copyright (c) 2014 HeyLets. All rights reserved. | |
// | |
#import "HLPlaceResourceManager.h" | |
#import "APIresources.h" | |
#import "UserProfileMan.h" | |
#import "CDPlace.h" | |
#import "CDPlace+mapping.h" | |
#import "LocationMan.h" | |
@implementation HLPlaceResourceManager | |
+ (id)sharedInstance { | |
static HLPlaceResourceManager *__sharedInstance; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
__sharedInstance = [[self alloc] init]; | |
[__sharedInstance setupResponseDescriptors]; | |
}); | |
return __sharedInstance; | |
} | |
- (void)fetchPlaceWithID:(NSNumber *)placeID | |
success:(void (^)(CDPlace *place))success | |
failure:(void (^)(NSError *error))failure | |
{ | |
[[HLRestKitManager sharedManager] getObjectsAtPath:[self buildPlacePathwithID:placeID] | |
parameters:[self buildParamsForGETPlaceRequest] | |
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { | |
CDPlace *place = mappingResult.dictionary[@"place"][0]; | |
if (success) | |
success(place); | |
} failure:^(RKObjectRequestOperation *operation, NSError *error) { | |
if (failure) | |
failure(error); | |
}]; | |
} | |
- (NSString *)buildPlacePathwithID:(NSNumber *)placeID { | |
return [NSString stringWithFormat:@"place/%@", placeID]; | |
} | |
- (NSDictionary *)buildParamsForGETPlaceRequest | |
{ | |
return @{ | |
@"lat": [LocationMan get].lat, | |
@"lon": [LocationMan get].lon | |
}; | |
} | |
- (void)setupResponseDescriptors { | |
RKResponseDescriptor *placeResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[CDPlace responseMapping] | |
method:RKRequestMethodGET | |
pathPattern:API_GET_PLACE | |
keyPath:API_PLACE_ROOTKEYPATH | |
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; | |
[[RKObjectManager sharedManager] addResponseDescriptor:placeResponseDescriptor]; | |
} | |
@end |
This file contains 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
@interface HLRestKitManager : RKObjectManager | |
+ (instancetype)sharedManager; | |
- (void)setupRouters; | |
- (void)setupRequestDescriptors; | |
- (void)setupResponseDescriptors; | |
- (NSManagedObjectContext *)newChildManagedObjectContext; | |
@en |
This file contains 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
// | |
// HLBaseObjectManager.m | |
// HeyLets2 | |
// | |
// Created by Alex Bush on 11/13/14. | |
// Copyright (c) 2014 HeyLets. All rights reserved. | |
// | |
#import "HLRestKitManager.h" | |
#import <RestKit/RestKit.h> | |
#define SQLITE_DB_NAME @"HeyLets.sqlite" | |
@implementation HLRestKitManager | |
+ (instancetype)sharedManager { | |
static HLRestKitManager *sharedManager = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
NSURL *url = [NSURL URLWithString:API_BASEURL]; | |
sharedManager = [self managerWithBaseURL:url]; | |
sharedManager.requestSerializationMIMEType = RKMIMETypeJSON; | |
[[self HTTPClient] setDefaultHeader:API_PARAMNAME_USERTOKEN value:kUserToken]; | |
[sharedManager setupPersistanceCoordinator]; | |
[sharedManager setupRouters]; | |
[sharedManager setupRequestDescriptors]; | |
[sharedManager setupResponseDescriptors]; | |
}); | |
return sharedManager; | |
} | |
- (void)setupPersistanceCoordinator { | |
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"HeyLets" withExtension:@"momd"]; | |
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy]; | |
NSPersistentStoreCoordinator *persistentStore = [self setupPersistentStoreCoordinatorToModel:managedObjectModel]; | |
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithPersistentStoreCoordinator:persistentStore]; | |
[managedObjectStore createManagedObjectContexts]; | |
self.managedObjectStore = managedObjectStore; | |
[RKManagedObjectStore setDefaultStore:managedObjectStore]; | |
} | |
- (NSPersistentStoreCoordinator *)setupPersistentStoreCoordinatorToModel:(NSManagedObjectModel*)model { | |
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:SQLITE_DB_NAME]; | |
NSURL *storeURL = [NSURL fileURLWithPath:storePath]; | |
NSError *error = nil; | |
NSPersistentStoreCoordinator* persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; | |
//Setup lightweight migration options | |
NSDictionary *options = @{ | |
NSMigratePersistentStoresAutomaticallyOption : @YES, | |
NSInferMappingModelAutomaticallyOption : @YES | |
}; | |
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) { | |
NSLog(@"Unresolved error %@, %@", error, [error userInfo]); | |
abort(); | |
} | |
return persistentStoreCoordinator; | |
} | |
- (void)setupRouters {} | |
- (void)setupRequestDescriptors {} | |
- (void)setupResponseDescriptors {} | |
- (NSManagedObjectContext *)newChildManagedObjectContext { | |
NSManagedObjectContext *managedObjectContext = [[RKManagedObjectStore defaultStore] newChildManagedObjectContextWithConcurrencyType:NSPrivateQueueConcurrencyType | |
tracksChanges:YES]; | |
return managedObjectContext; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment