Created
September 29, 2011 19:14
-
-
Save geoff-parsons/1251650 to your computer and use it in GitHub Desktop.
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
@implementation MyAppDelegate | |
- (void) setUpDataMappingForObjectManager: (RKObjectManager*) objectManager | |
{ | |
// Workouts | |
RKManagedObjectMapping* workoutMapping = [RKManagedObjectMapping mappingForClass: [Workout class]]; | |
workoutMapping.primaryKeyAttribute = @"workoutID"; | |
[workoutMapping mapKeyPath: @"id" toAttribute: @"workoutID"]; | |
[workoutMapping mapKeyPath: @"name" toAttribute: @"name"]; | |
[workoutMapping mapKeyPath: @"description" toAttribute: @"workoutDescription"]; | |
[workoutMapping mapKeyPath: @"activities" toAttribute: @"activities"]; | |
[objectManager.mappingProvider setMapping: workoutMapping forKeyPath: @"workout"]; | |
// Activities | |
RKManagedObjectMapping* activityMapping = [RKManagedObjectMapping mappingForClass: [Activity class]]; | |
activityMapping.primaryKeyAttribute = @"activityID"; | |
[activityMapping mapKeyPath: @"id" toAttribute: @"activityID"]; | |
[activityMapping mapKeyPath: @"name" toAttribute: @"name"]; | |
[activityMapping mapKeyPath: @"description" toAttribute: @"activityDescription"]; | |
[activityMapping mapRelationship: @"workout" withMapping: workoutMapping]; | |
[objectManager.mappingProvider setMapping: activityMapping forKeyPath: @"activity"]; | |
} | |
- (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions | |
{ | |
// View initialization stuff... | |
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL: @"http://localhost:3000"]; | |
objectManager.client.username = [userSettings stringForKey: @"email_address_preference"]; | |
objectManager.client.password = [userSettings stringForKey: @"password_preference"]; | |
// Enable automatic network activity indicator management | |
objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES; | |
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename: @"MyRestKitApp.sqlite" | |
usingSeedDatabaseName: nil | |
managedObjectModel: nil | |
delegate: self]; | |
[self setUpDataMappingForObjectManager: objectManager]; | |
[userSettings release]; | |
return YES; | |
} | |
@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
[ | |
{ | |
"created_at":"2011-08-06T20:47:37Z", | |
"description":"Bacon ipsum dolor sit amet boudin pork chop ball tip, t-bone bresaola tongue jowl meatball meatloaf. T-bone shank brisket ground round flank beef, venison pork loin tri-tip pancetta pig shor.", | |
"id":32, | |
"name":"Workout Foo", | |
"updated_at":"2011-08-06T20:47:37Z", | |
"activities": [ | |
{ | |
"name":"Squats", | |
"description":"Bacon ipsum dolor sit amet boudin pork chop ball tip, t-bone bresaola tongue jowl meat.", | |
"duration":1872 | |
}, | |
{ | |
"name":"Pushups", | |
"description":"Bacon ipsum dolor sit amet boudin pork chop ball tip, t-bone bresaola tongue jowl meatball meatloaf..", | |
"count":139 | |
}, | |
{ | |
"name":"Pullups", | |
"description":"Bacon ipsum dolor sit amet boudin pork chop ball tip, t-bone bresaola tongue jowl meatball meatloaf. T-bone shank brisket ground.", | |
"duration":402, | |
"count":61 | |
} | |
] | |
} | |
] |
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
@implementation WorkoutsTableViewController | |
@synthesize workouts; | |
- (void) loadObjectsFromDataStore | |
{ | |
NSDate* lastUpdatedAt = [[NSUserDefaults standardUserDefaults] objectForKey: @"last_updated_workouts_at"]; | |
NSDate* oneHourAgo = [[NSDate alloc] initWithTimeIntervalSinceNow: -3600]; | |
if ( lastUpdatedAt == nil || [lastUpdatedAt compare: oneHourAgo] == NSOrderedAscending ) { | |
[self loadDataFromWebService]; | |
} else { | |
[workouts release]; | |
NSFetchRequest* request = [Workout fetchRequest]; | |
NSSortDescriptor* descriptor = [NSSortDescriptor sortDescriptorWithKey: @"name" | |
ascending: YES]; | |
[request setSortDescriptors: [NSArray arrayWithObject: descriptor]]; | |
workouts = [[Workout objectsWithFetchRequest: request] retain]; | |
} | |
[oneHourAgo release]; | |
} | |
- (void) loadDataFromWebService | |
{ | |
// Load the object model via RestKit | |
RKObjectManager* objectManager = [RKObjectManager sharedManager]; | |
[objectManager loadObjectsAtResourcePath: @"/workouts.json" | |
delegate: self | |
block: ^(RKObjectLoader* loader) { | |
// Workouts are returned as a naked array in JSON, so we instruct the loader | |
// to user the appropriate object mapping | |
loader.objectMapping = [objectManager.mappingProvider objectMappingForClass: [Workout class]]; | |
}]; | |
} | |
#pragma mark View Lifecycle Methods | |
- (void) viewDidAppear: (BOOL) animated | |
{ | |
[super viewDidAppear: animated]; | |
[self loadObjectsFromDataStore]; | |
} | |
#pragma mark RestKit Object Loader Delegate Methods | |
- (void) objectLoader: (RKObjectLoader *) objectLoader | |
didFailWithError: (NSError *) error | |
{ | |
UIAlertView* alert = [[[UIAlertView alloc] initWithTitle: @"Error" | |
message: [error localizedDescription] | |
delegate: nil | |
cancelButtonTitle: @"OK" | |
otherButtonTitles: nil] autorelease]; | |
[alert show]; | |
} | |
- (void) objectLoader: (RKObjectLoader*) objectLoader | |
didLoadObjects: (NSArray*) objects | |
{ | |
[[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: @"last_updated_workouts_at"]; | |
[[NSUserDefaults standardUserDefaults] synchronize]; | |
[self loadObjectsFromDataStore]; | |
[workoutsTableView reloadData]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment