Skip to content

Commit

Permalink
Initial implementation of MagicalRecord + Core Data integration example
Browse files Browse the repository at this point in the history
  • Loading branch information
blakewatters committed Jan 4, 2013
1 parent 49dfff2 commit 1bbec8d
Show file tree
Hide file tree
Showing 10 changed files with 1,261 additions and 627 deletions.
17 changes: 17 additions & 0 deletions .gitignore
@@ -0,0 +1,17 @@
# temp nibs and swap files
*~.nib
*.swp
*.orig

# OS X folder attributes
.DS_Store

# user-specific XCode stuff
*.mode1v3
*.mode2v3
*.pbxuser
*.perspectivev3
*.xcuserdatad

# CocoaPods
Pods
4 changes: 4 additions & 0 deletions Podfile
@@ -0,0 +1,4 @@
platform :ios, '5.0'

pod 'RestKit', '~> 0.20.0pre'
pod 'MagicalRecord', '~> 2.0'
36 changes: 36 additions & 0 deletions Podfile.lock
@@ -0,0 +1,36 @@

PODS:
- AFNetworking (1.1.0)
- MagicalRecord (2.1.beta.1)
- RestKit (0.20.0pre6):
- RestKit/Core (= 0.20.0pre6)
- RestKit/Core (0.20.0pre6):
- RestKit/CoreData
- RestKit/Network
- RestKit/ObjectMapping
- RestKit/CoreData (0.20.0pre6)
- RestKit/Network (0.20.0pre6):
- AFNetworking (= 1.1.0)
- RestKit/ObjectMapping
- RestKit/Support
- SOCKit
- RestKit/ObjectMapping (0.20.0pre6)
- RestKit/Support (0.20.0pre6)
- SOCKit (1.1)

DEPENDENCIES:
- MagicalRecord (~> 2.0)
- RestKit (~> 0.20.0pre)

SPEC CHECKSUMS:
AFNetworking: 761a418233d280c1e0580bcb76ff9cb4c3073d17
MagicalRecord: 586523b576a7b8e7d5a5de301f7ea3186f9d1101
RestKit: ccca19640fcc3031f82ad4250c44061e2e2ddce7
RestKit/Core: ccca19640fcc3031f82ad4250c44061e2e2ddce7
RestKit/CoreData: ccca19640fcc3031f82ad4250c44061e2e2ddce7
RestKit/Network: ccca19640fcc3031f82ad4250c44061e2e2ddce7
RestKit/ObjectMapping: ccca19640fcc3031f82ad4250c44061e2e2ddce7
RestKit/Support: ccca19640fcc3031f82ad4250c44061e2e2ddce7
SOCKit: 179515c1a621dd5652a60e9be02ea4adcd25bc9b

COCOAPODS: 0.16.0
1,654 changes: 1,162 additions & 492 deletions RKMagicalRecord.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions RKMagicalRecord.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions RKMagicalRecord/RKMRAppDelegate.h
Expand Up @@ -12,11 +12,4 @@

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

@end
143 changes: 18 additions & 125 deletions RKMagicalRecord/RKMRAppDelegate.m
Expand Up @@ -6,146 +6,39 @@
// Copyright (c) 2013 RestKit. All rights reserved.
//

#import <RestKit/RestKit.h>
#import "CoreData+MagicalRecord.h"
#import "RKMRAppDelegate.h"

#import "RKMRMasterViewController.h"

@implementation RKMRAppDelegate

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Setup MagicalRecord
[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"RKMagicalRecord.sqlite"];

/**
Configure RestKit to share a Persistent Store Coordinator with MagicalRecord. This ensures that object request operations will persist back to the same Persistent Store managed by MagicalRecord, making managed objects available across the libraries.
*/
NSPersistentStoreCoordinator *persistentStoreCoordinator = [NSPersistentStoreCoordinator MR_defaultStoreCoordinator];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithPersistentStoreCoordinator:persistentStoreCoordinator];
[managedObjectStore createManagedObjectContexts];

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://restkit.org"]];
objectManager.managedObjectStore = managedObjectStore;

// Pass the MagicalRecord
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
RKMRMasterViewController *controller = (RKMRMasterViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
controller.managedObjectContext = [NSManagedObjectContext MR_defaultContext];
return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
// Saves changes in the application's managed object context before the application terminates.
[self saveContext];
}

- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}

#pragma mark - Core Data stack

// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}

// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"RKMagicalRecord" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}

// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"RKMagicalRecord.sqlite"];

NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

return _persistentStoreCoordinator;
}

#pragma mark - Application's Documents directory

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
[MagicalRecord cleanUp];
}

@end
9 changes: 9 additions & 0 deletions RKMagicalRecord/RKMagicalRecord-Prefix.pch
Expand Up @@ -13,3 +13,12 @@
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#endif

#import <Availability.h>
#if __IPHONE_OS_VERSION_MIN_REQUIRED
#import <SystemConfiguration/SystemConfiguration.h>
#import <MobileCoreServices/MobileCoreServices.h>
#else
#import <SystemConfiguration/SystemConfiguration.h>
#import <CoreServices/CoreServices.h>
#endif
@@ -1,9 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model name="Test1.xcdatamodel" userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="1" systemVersion="11A491" minimumToolsVersion="Automatic" macOSVersion="Automatic" iOSVersion="Automatic">
<model name="" userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="1811" systemVersion="12C60" minimumToolsVersion="Automatic" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="Event">
<attribute name="timeStamp" optional="YES" attributeType="Date"/>
<attribute name="name" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="timeStamp" optional="YES" attributeType="Date">
<userInfo/>
</attribute>
<userInfo/>
</entity>
<elements>
<element name="Event" positionX="261" positionY="189" width="128" height="60"/>
<element name="Event" positionX="261" positionY="189" width="128" height="75"/>
</elements>
</model>

0 comments on commit 1bbec8d

Please sign in to comment.