Created
December 7, 2012 04:05
-
-
Save jlee42/4230686 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
Log: | |
response.body=[{"id":7,"name":"Campus Life"}] | |
2012-12-06 22:00:20.442 TigerTracker[34518:5203] D restkit.object_mapping:RKMapperOperation.m:338 Performing object mapping sourceObject: ( | |
{ | |
id = 7; | |
name = "Campus Life"; | |
} | |
) | |
and targetObject: (null) | |
2012-12-06 22:00:20.443 TigerTracker[34518:c07] It Failed: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "Unable to find any mappings for the given content" UserInfo=0xf185130 {DetailedErrors=( | |
), NSLocalizedDescription=Unable to find any mappings for the given content, keyPath=null} | |
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
// Load the object model via RestKit | |
RKObjectManager* objectManager = [RKObjectManager sharedManager]; | |
[objectManager getObjectsAtPath:@"customers" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) | |
{ | |
NSLog(@"It worked: %@", [mappingResult array]); | |
// Or if you're only expecting a single object: | |
NSLog(@"It Worked: %@", [mappingResult firstObject]); | |
} failure:^(RKObjectRequestOperation *operation, NSError *error) | |
{ | |
NSLog(@"It Failed: %@", error); | |
}]; |
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 | |
NSString *baseURLString = [NSString stringWithFormat:@"%@://%@:%@/api/v2/json/%@/", | |
[NSObject TT_PROTOCOL], [NSObject TT_HOST], [NSObject TT_PORT], [[NSUserDefaults standardUserDefaults] valueForKey:@"auth_token"]]; | |
NSURL *baseURL = [NSURL URLWithString:baseURLString]; | |
NSLog(@"baseURLString: %@", baseURLString); | |
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL]; | |
// Set JSON as the default response type. | |
[[RKObjectManager sharedManager].HTTPClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; | |
[[RKObjectManager sharedManager].HTTPClient setDefaultHeader:@"Accept" value:@"application/json"]; | |
// 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; | |
// Setup our object mappings | |
RKEntityMapping *customerMapping = [RKEntityMapping mappingForEntityForName:@"TTCustomer" inManagedObjectStore:managedObjectStore]; | |
customerMapping.identificationAttributes = @[ @"customerID" ]; | |
[customerMapping addAttributeMappingsFromDictionary:@{ | |
@"id": @"customerID", | |
@"name": @"name", | |
}]; | |
customerMapping.setDefaultValueForMissingAttributes = YES; | |
customerMapping.setNilForMissingRelationships = YES; | |
RKEntityMapping *activityTypeMapping = [RKEntityMapping mappingForEntityForName:@"TTActivityType" inManagedObjectStore:managedObjectStore]; | |
activityTypeMapping.identificationAttributes = @[ @"activityTypeID" ]; | |
[activityTypeMapping addAttributeMappingsFromDictionary:@{ | |
@"id": @"activityTypeID", | |
@"name": @"name", | |
@"abbreviation": @"abbreviation", | |
@"customer_id": @"customerID", | |
}]; | |
activityTypeMapping.setDefaultValueForMissingAttributes = YES; | |
activityTypeMapping.setNilForMissingRelationships = YES; | |
// Register our mappings with the provider | |
RKResponseDescriptor *customerResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:customerMapping | |
pathPattern:@"/customers" | |
keyPath:nil | |
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; | |
[objectManager addResponseDescriptor:customerResponseDescriptor]; | |
RKResponseDescriptor *activityTypeResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:activityTypeMapping | |
pathPattern:@"/customers/:customerID/activity_types" | |
keyPath:nil | |
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; | |
[objectManager addResponseDescriptor:activityTypeResponseDescriptor]; | |
// // Create mapping for Activities | |
// RKManagedObjectMapping* activityMapping = [RKManagedObjectMapping mappingForClass:[TTActivity class] inManagedObjectStore:objectManager.objectStore]; | |
// [activityMapping setRootKeyPath:@""]; | |
// [activityMapping setPrimaryKeyAttribute:@"activityID"]; | |
// [activityMapping mapKeyPathsToAttributes:@"id", @"activityID", | |
// @"time_in", @"timeIn", | |
// @"time_out", @"timeOut", | |
// @"customer_id", @"customerID", | |
// @"user_id", @"userID", | |
// @"updated_at", @"updatedAt", | |
// @"created_at", @"createdAt", | |
// @"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", | |
// nil]; | |
// | |
// activityMapping.setDefaultValueForMissingAttributes = YES; | |
// activityMapping.setNilForMissingRelationships = YES; | |
// | |
// // Create mapping for Users | |
// RKManagedObjectMapping* userMapping = [RKManagedObjectMapping mappingForClass:[TTUser class] inManagedObjectStore:objectManager.objectStore]; | |
// [userMapping setRootKeyPath:@""]; | |
// [userMapping setPrimaryKeyAttribute:@"userID"]; | |
// [userMapping mapKeyPathsToAttributes:@"id", @"userID", | |
// @"full_name", @"fullName", | |
// @"display_name", @"displayName", | |
// @"username", @"username", | |
// @"lsuid", @"userLSUID", | |
// @"photo_hash", @"photoHash", | |
// nil]; | |
// userMapping.setDefaultValueForMissingAttributes = YES; | |
// userMapping.setNilForMissingRelationships = YES; | |
// | |
// [[RKObjectManager sharedManager].mappingProvider setObjectMapping:activityTypeMapping forResourcePathPattern:@"/activity_types"]; | |
// [[RKObjectManager sharedManager].mappingProvider setObjectMapping:customerMapping forResourcePathPattern:@"/customers"]; | |
// NSString *activitiesPath = [NSString stringWithFormat:@"/activity_types/%@/activities", | |
// [[NSUserDefaults standardUserDefaults] valueForKey:@"activity_type_id"]]; | |
// [[RKObjectManager sharedManager].mappingProvider setObjectMapping:activityMapping forResourcePathPattern:activitiesPath]; | |
// [[RKObjectManager sharedManager].mappingProvider setMapping:userMapping forKeyPath:@"user"]; | |
// | |
// [activityMapping mapRelationship:@"user" withMapping:userMapping]; | |
// [activityMapping connectRelationship:@"user" withMapping:userMapping fromKeyPath:@"userID" toKeyPath:@"userID"]; | |
// [[RKObjectManager sharedManager].mappingProvider setMapping:activityMapping forKeyPath:@""]; | |
// | |
// [activityTypeMapping mapRelationship:@"customer" withMapping:customerMapping]; | |
// [activityTypeMapping connectRelationship:@"customer" withMapping:customerMapping fromKeyPath:@"customerID" toKeyPath:@"customerID"]; | |
// [[RKObjectManager sharedManager].mappingProvider setMapping:activityTypeMapping forKeyPath:@"activityType.customer"]; | |
/** | |
Complete Core Data stack initialization | |
**/ | |
[managedObjectStore createPersistentStoreCoordinator]; | |
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"TigerTrackerData.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