small medium large xlarge

23 Nov 2012, 22:39
Mel Torme (5 posts)

First of all, I’m incredibly sorry if this has already been addressed elsewhere, but I’ve been reading through the book and these forums and I haven’t found a decent solution yet.

How do I work with Core Data in a Document-based application in Mac OS X? What parts of the Core Data structure are automatically created when using a Document-based app and which do I need to initialize? I assume I still need to establish a Persistent Store, but how do I reference it? Is it automatically an SQLite store?

Chapter 10 (Dynamic Properties) makes some reference to NSPersistentDocument, but I’m unclear as to how I’m supposed to configure things. What’s the proper way to access the Core Data persistent store from the various classes in my document?

Through which chapters should I read in order to gain the best understanding of the proper way to initialize and manipulate Core Data in a document-based Mac OS X app?

Thank you in advance for your assistance!

01 Jul 2014, 20:19
Marcus S. Zarra (284 posts)

This has not been covered in depth because it is not a commonly used feature of OS X/Core Data and because there is not much of a variant.

Effectively you use NSPersistentDocument instead of NSDocument. The NSPersistentDocument will create and manage the Core Data stack for you.

From there you treat it like a normal document.

03 Dec 2012, 20:39
Mel Torme (5 posts)

Marcus - Thank you for your reply. If you ever decide to change your mind, I believe there’s plenty that can be discussed.

Just for starters, consider the following: 1) I understand the ManagedObjectContext and PersistentStore are created automatically for each document, so: a) how would you change the ManagedObjectContext’s concurrency type to “NSMainQueueConcurrencyType” if it’s already been initialized. b) how would you change the PersistentStore to “NSSQLiteStoreType” when configurePersistentStoreCoordinatorForURL: doesn’t get called for new, unsaved documents?

2) Why can’t a ManagedObjectContext in a NSPersistentDocument respond to the save: request?

…which may answer…

3) Why can’t a NSPersistentDocument follow your multi-threading recommendations from the book? Any locally-created ManagedObjectContext in an NSOperation, despite being connected to the same persistent store as the main document, returns a fault when attempting to access an entity’s properties. I assume this is because ManagedObjects added by the document are added to the document’s ManagedObjectContext and not persisted to the store.

Pardon the pun, but you could totally write the book on NSPersistentDocuments. Especially considering Apple’s NSPersistentDocument tutorial is from 10.4 and is labeled “Not Recommended”.

10 Oct 2013, 01:07
Duncan Groenewald (12 posts)

Hey Mel, if you still struggling with this I have a couple of apps on App Store that use NSPersistentDocument, there are free versions you can download and try out, if this is what you are trying to achieve let me know and I’ll write up some how to’s for you. They also have iPad/iPhone versions that use the same files and which can use iCloud to replicate the files or you can just copy using iTunes. Note that at this stage I just replicate the database file like it is a document, too much bad advice on how to setup the transaction logging that sent me down ratholes earlier.

Am just busy converting to use Core Data transaction replication, so far its been just as robust - once I realised I need to ignore all the ‘expert’ advice given freely on the web. If you are still interested look for the apps by OSSH Pty Ltd in App store or iTunes and send me an enquiry via the support page using ‘Mel Torme’ and I will be happy to help you.

Cheers Duncan

20 Mar 2014, 10:23
Motti Shneor (1 post)

Well, I’m not Mell, but I DEFINITELY need those “how to’s” and even worse — an entry point for NSPersistentDocument based application writing.

The documentation for CoreData Document based applications is so old and bad, it does not address even the simplest of flows.

I tried using the documentation in the header files, but I’m pretty stuck too — Here’s just a simple novice question. Following a user’s action, I do a big cycle of calculations that result in a series of related entities created.

Now I want to automatically save these changes, but calling save on the document’s context results in exceptions from time to time — I think it clashes with the “autosave” mechanism which tries to commit the changes at the same time.

So I told the document to save — and it hung the application!!! (sometimes) for no obvious reason. Pausing in the debugger tells me there’s some deadlock in the “save” method. Who’s lock is it? I didn’t create one, or created a thread, or anything like it.

  1. I didn’t find anywhere (not even in the headers) the Delegate protocol of NSPersistentDocument. I
  2. I didn’t find HOW TO GET validation errors (inter-property) BEFORE committing to the persistent store.

I’ll stop here, because I could go on forever. I need at least a reasonable code sample of such app, or a few how-to’s

Thanks for reading my complaints….

01 Jul 2014, 20:20
Marcus S. Zarra (284 posts)

I suspect there are radars to be filed in this area and I will definitely look into the whole NSPersistentDocument state when I do the next refresh. It may be possible that it is like UIManagedDocument and it turns into something to stay away from and build your own.

I will definitely explore it.

You must be logged in to comment