Skip to content

Instantly share code, notes, and snippets.

@lattejed
Last active August 29, 2015 13:56
Show Gist options
  • Save lattejed/9286115 to your computer and use it in GitHub Desktop.
Save lattejed/9286115 to your computer and use it in GitHub Desktop.
// Example app delegate for RestKit + Core Data config
// Import Core Data and RestKit, in that order
#import <CoreData/CoreData.h>
#import <RestKit/RestKit.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Configure the global object manager with your API's base url
NSURL *baseURL = [NSURL URLWithString:@"https://your-api.example.com"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
// Configure Core Data
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"YourDB.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);
// Configure an in memory object cache
[managedObjectStore createManagedObjectContexts];
managedObjectStore.managedObjectCache =
[[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
// Turn on logging
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
// Configure RestKit mappings
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application;
{
NSError* error = nil;
if (![[RKObjectManager sharedManager].managedObjectStore.mainQueueManagedObjectContext saveToPersistentStore:&error])
{
NSLog(@"Failed to persist Core Data with error: %@", error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment