Created
May 15, 2013 15:22
-
-
Save JJSaccolo/5584804 to your computer and use it in GitHub Desktop.
Core Data with a Single Shared UIManagedDocument
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
| #import <Foundation/Foundation.h> | |
| typedef void (^OnDocumentReady) (UIManagedDocument *document); | |
| @interface MYDocumentHandler : NSObject | |
| @property (strong, nonatomic) UIManagedDocument *document; | |
| + (MYDocumentHandler *)sharedDocumentHandler; | |
| - (void)performWithDocument:(OnDocumentReady)onDocumentReady; | |
| @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
| #import "MYDocumentHandler.h" | |
| @interface MYDocumentHandler () | |
| - (void)objectsDidChange:(NSNotification *)notification; | |
| - (void)contextDidSave:(NSNotification *)notification; | |
| @end; | |
| @implementation MYDocumentHandler | |
| @synthesize document = _document; | |
| static MYDocumentHandler *_sharedInstance; | |
| + (MYDocumentHandler *)sharedDocumentHandler | |
| { | |
| static dispatch_once_t once; | |
| dispatch_once(&once, ^{ | |
| _sharedInstance = [[self alloc] init]; | |
| }); | |
| return _sharedInstance; | |
| } | |
| - (id)init | |
| { | |
| self = [super init]; | |
| if (self) { | |
| NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject]; | |
| url = [url URLByAppendingPathComponent:@"MyDocument.md"]; | |
| self.document = [[UIManagedDocument alloc] initWithFileURL:url]; | |
| // Set our document up for automatic migrations | |
| NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: | |
| [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, | |
| [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; | |
| self.document.persistentStoreOptions = options; | |
| // Register for notifications | |
| [[NSNotificationCenter defaultCenter] addObserver:self | |
| selector:@selector(objectsDidChange:) | |
| name:NSManagedObjectContextObjectsDidChangeNotification | |
| object:self.document.managedObjectContext]; | |
| [[NSNotificationCenter defaultCenter] addObserver:self | |
| selector:@selector(contextDidSave:) | |
| name:NSManagedObjectContextDidSaveNotification | |
| object:self.document.managedObjectContext]; | |
| } | |
| return self; | |
| } | |
| - (void)performWithDocument:(OnDocumentReady)onDocumentReady | |
| { | |
| void (^OnDocumentDidLoad)(BOOL) = ^(BOOL success) { | |
| onDocumentReady(self.document); | |
| }; | |
| if (![[NSFileManager defaultManager] fileExistsAtPath:[self.document.fileURL path]]) { | |
| [self.document saveToURL:self.document.fileURL | |
| forSaveOperation:UIDocumentSaveForCreating | |
| completionHandler:OnDocumentDidLoad]; | |
| } else if (self.document.documentState == UIDocumentStateClosed) { | |
| [self.document openWithCompletionHandler:OnDocumentDidLoad]; | |
| } else if (self.document.documentState == UIDocumentStateNormal) { | |
| OnDocumentDidLoad(YES); | |
| } | |
| } | |
| - (void)objectsDidChange:(NSNotification *)notification | |
| { | |
| #ifdef DEBUG | |
| NSLog(@"NSManagedObjects did change."); | |
| #endif | |
| } | |
| - (void)contextDidSave:(NSNotification *)notification | |
| { | |
| #ifdef DEBUG | |
| NSLog(@"NSManagedContext did save."); | |
| #endif | |
| } | |
| @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
| #import "MYDocumentHandler.h" | |
| @implementation MyViewController | |
| - (void)viewWillAppear:(BOOL)animated | |
| { | |
| [super viewWillAppear:animated]; | |
| if (!self.document) { | |
| [[MYDocumentHandler sharedDocumentHandler] performWithDocument:^(UIManagedDocument *document) { | |
| self.document = document; | |
| // Do stuff with the document, set up a fetched results controller, whatever. | |
| }]; | |
| } | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment