Created
December 6, 2012 16:46
-
-
Save jlee42/4225953 to your computer and use it in GitHub Desktop.
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
- (void)initializeRestKit | |
{ | |
// Set up RestKit Logging | |
RKLogConfigureByName("RestKit", RKLogLevelTrace); | |
RKLogConfigureByName("RestKit/Network*", RKLogLevelTrace); | |
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace); | |
// Initialize RestKit | |
NSURL *baseURL = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] valueForKey:@"application_base_url"]]; | |
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL]; | |
// Enable Activity Indicator Spinner | |
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES; | |
// Initialize managed object store | |
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil]; | |
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel]; | |
objectManager.managedObjectStore = managedObjectStore; | |
[managedObjectStore createManagedObjectContexts]; | |
// Setup our object mappings | |
/** | |
Mapping by entity. Here we are configuring a mapping by targetting a Core Data entity with a specific | |
name. This allows us to map back Twitter user objects directly onto NSManagedObject instances -- | |
there is no backing model class! | |
*/ | |
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore]; | |
userMapping.identificationAttributes = @[ @"userID" ]; | |
userMapping.setNilForMissingRelationships = YES; | |
userMapping.setDefaultValueForMissingAttributes = YES; | |
[userMapping addAttributeMappingsFromDictionary:@{ | |
@"id": @"userID", | |
@"full_name": @"fullName", | |
@"display_name": @"displayName", | |
@"username": @"username", | |
@"lsuid": @"userLSUID", | |
@"photo_hash": @"photoHash", | |
}]; | |
RKEntityMapping *activityTypeMapping = [RKEntityMapping mappingForEntityForName:@"ActivityType" inManagedObjectStore:managedObjectStore]; | |
activityTypeMapping.identificationAttributes = @[ @"activityTypeID" ]; | |
activityTypeMapping.setNilForMissingRelationships = YES; | |
activityTypeMapping.setDefaultValueForMissingAttributes = YES; | |
[activityTypeMapping addAttributeMappingsFromDictionary:@{ | |
@"id": @"activityTypeID", | |
@"name": @"name", | |
@"abbreviation": @"abbreviation", | |
@"customer_id": @"customerID", | |
}]; | |
RKEntityMapping *activityMapping = [RKEntityMapping mappingForEntityForName:@"Activity" inManagedObjectStore:managedObjectStore]; | |
activityMapping.identificationAttributes = @[ @"activityID" ]; | |
activityMapping.setNilForMissingRelationships = YES; | |
activityMapping.setDefaultValueForMissingAttributes = YES; | |
userMapping.identificationAttributes = @[ @"userID" ]; | |
[activityMapping addAttributeMappingsFromDictionary:@{ | |
@"id": @"activityID", | |
@"created_at": @"createdAt", | |
@"updated_at": @"updatedAt", | |
@"time_in": @"timeIn", | |
@"time_out": @"timeOut", | |
@"customer_id": @"customerID", | |
@"user_id": @"userID", | |
@"term_id": @"termID", | |
@"course_id": @"courseID", | |
@"personnel_id": @"personnelID", | |
@"comment_in": @"commentIn", | |
@"comment_out": @"commentOut", | |
@"station_id": @"stationID", | |
@"activity_topic_id": @"activityTopicID", | |
@"activity_type_id": @"activityTypeID", | |
}]; | |
[activityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"user" toKeyPath:@"user" withMapping:userMapping]]; | |
// Update date format so that we can parse Twitter dates properly | |
// Wed Sep 29 15:31:08 +0000 2010 | |
[RKObjectMapping addDefaultDateFormatterForString:@"E MMM d HH:mm:ss Z y" inTimeZone:nil]; | |
// Register our mappings with the provider | |
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:activityMapping | |
pathPattern:@"/activity_types/:activityTypeID/activities/:activityID" | |
keyPath:nil | |
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; | |
[objectManager addResponseDescriptor:responseDescriptor]; | |
// Uncomment this to use XML, comment it to use JSON | |
// objectManager.acceptMIMEType = RKMIMETypeXML; | |
// [objectManager.mappingProvider setMapping:statusMapping forKeyPath:@"statuses.status"]; | |
// Database seeding is configured as a copied target of the main application. There are only two differences | |
// between the main application target and the 'Generate Seed Database' target: | |
// 1) RESTKIT_GENERATE_SEED_DB is defined in the 'Preprocessor Macros' section of the build setting for the target | |
// This is what triggers the conditional compilation to cause the seed database to be built | |
// 2) Source JSON files are added to the 'Generate Seed Database' target to be copied into the bundle. This is required | |
// so that the object seeder can find the files when run in the simulator. | |
/** | |
Complete Core Data stack initialization | |
*/ | |
[managedObjectStore createPersistentStoreCoordinator]; | |
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"TigerTracker.sqlite"]; | |
NSError *error; | |
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error]; | |
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error); | |
// Create the managed object contexts | |
[managedObjectStore createManagedObjectContexts]; | |
// Configure a managed object cache to ensure we do not create duplicate objects | |
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment