Skip to content

Instantly share code, notes, and snippets.

@smic
Created January 25, 2013 06:55
Show Gist options
  • Save smic/4632383 to your computer and use it in GitHub Desktop.
Save smic/4632383 to your computer and use it in GitHub Desktop.
Replace the managed object context of a NSPersistentDocument by a context created by MagicalRecord.
#import <Cocoa/Cocoa.h>
@interface SMDocument : NSPersistentDocument
@end
#import "SMDocument.h"
@implementation SMDocument
- (id)init {
self = [super init];
if (self) {
NSManagedObjectContext *context = self.managedObjectContext;
if (context.concurrencyType != NSMainQueueConcurrencyType) {
NSUndoManager *undoManager = context.undoManager;
NSPersistentStoreCoordinator *coordinator = context.persistentStoreCoordinator;
context = [NSManagedObjectContext newMainQueueContext];
context.persistentStoreCoordinator = coordinator;
context.undoManager = undoManager;
self.managedObjectContext = context;
}
NSAssert1(self.managedObjectContext.concurrencyType == NSMainQueueConcurrencyType, @"Wrong concurrency type: %li", self.managedObjectContext.concurrencyType);
}
return self;
}
@end
@colasbd
Copy link

colasbd commented Feb 19, 2014

Great! Have you tested this idea? I would never have thought that setting the property managedObjectContext on NSDocument could be authorized (yes, it is said in the doc that you can...)!!!! How did you get this idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment