Nspersistentdocument Core Data Tutorial For Mac



  1. 'If you have a large data set or require a managed object model, you may want to use NSPersistentDocument to create a document-based app that uses the Core Data framework.' - Document-Based App Programming Guide for Mac 'Applications using very large data sets would likely benefit from a custom persistance implementation.
  2. “ Before Mac OS X, the Mac OS used ‘forked’ files, which have two components: a data fork and a resource fork. The Mac OS Standard (HFS) and Mac OS Extended (HFS Plus) disk formats support forked files. When you move these types of files to other disk formats, the resource fork can be lost.
  3. This post is a follow-up to another post I wrote on the very same subject. I am showing here the full implementation of a NSPersistentDocument based class that allows to use package documents embedding a Core Data store. I short, what this post adds to the previous one is: improved encapsulation; NSDocumentController subclass to correctly.

Mac OS X Tutorial Implementing TICoreDataSync in a Mac OS X non-document-based Core Data application. This tutorial walks through adding the TICoreDataSync framework to a very simple, non-document-based desktop application. The example app uses Dropbox sync, and is hard-wired to use a desktop Dropbox located at /Dropbox. In a shipping app, you. Sep 19, 2012 Armed with these basic principles we can get started with actual code. We’ll start a new Mac Cocoa app. We don’t check “Use Core Data” because that would cause the template to set up the app using NSPersistentDocument. Also we already specify our document extension here. This sets up a document type in the info of the app target.

I'm working on an OSX (Mac OS) document based app with Core Data.

I'm having problems importing in a PrivateContext. Importing in the MainContext works fine, but of course freezes the UI.


The app has a NSPersistentDocument (Document.h and .m).

In this document I have autosavesInPlace enabled (YES).


The Window belonging to the Document is added in makeWindowControllers

- (void)makeWindowControllers {

familyWindowController *myWindow = [[NSStoryboard storyboardWithName:@'Main' bundle:nil] instantiateControllerWithIdentifier:@'Document Window Controller'];

[self addWindowController: myWindow];

myWindow.managedObjectContext = self.managedObjectContext;

}


The WindowController gets his NSManagedObjectContext (MOC) from self.managedObjectContext.

This Window and MOC are given to childViewControllers, like the sheet I use for the import progress from a file.


The import sheet (ViewController) has a NSWindow property and a MOC property which is set by the prepareForSegue.

Next to the MOC from the Main window I have a PrivateContext (MOC).

It turn out that the originally created MOC is NOT of the NSMainQueueConcurrencyType. This is required is you want to use it with a parentContext.

Nspersistentdocument Core Data Tutorial For Mac

With this code I change it (maybe this is wrong?).

Tutorial

Nspersistentdocument Core Data Tutorial For Mac Download


Nspersistentdocument Core Data Tutorial For Macbook Pro

NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];

context.persistentStoreCoordinator = self.managedObjectContext.persistentStoreCoordinator;

[context setMergePolicy: NSMergeByPropertyStoreTrumpMergePolicy];

Core Data Tutorial Iphone

Nspersistentdocument core data tutorial for mac os

Nspersistentdocument Core Data Tutorial For Mac Os

_managedObjectContext = context; // _managedObjectContext is the local MOC from the WindowController


The privateContext is created with this code.
_privateContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];

_privateContext.parentContext = _managedObjectContext;

_privateContext.undoManager = nil;

I parse an XML with Google’s GDataXMLNode.

Core data objects are created with

Person *currentPersonObject = [NSEntityDescription insertNewObjectForEntityForName:@'Person' inManagedObjectContext:_managedObjectContext];

What works:

If I don’t change the MOC to a NSMainQueueConcurrencyType, everything works fine, but I have a frozen UI.

What don’t work:

- When I make the MOC into a NSMainQueueConcurrencyType, I can import the data, but it doesn’t save to the file. I suspect something is wrong with the persistentStore, but I don’t see what.

- When I use the PrivateContext (thru the Parent) I can get progress in the UI en UI updates, as long *** I don’t move the Window or trigger a Window refresh. I think this is because of the NSTableViews in the Window, which all use Core Data and binding. So when they refresh the view you get Core Data errors (out of bound…). No Idea how to fix this.

Any ideas are welcome.

An example with OSX NSDocument and Core Data would be even better