Duck
Duck

Reputation: 35933

Strange things creating a new document-based Cocoa application on Xcode

I am new to cocoa development.

I just created for the first time a new document type application using core data and notice a few strange things.

  1. AppDelegate is practically empty, there is no code to create the core data store, the managedObjectContext, nothing.

  2. Two files were added: Document.m and Document.h that I understand is the model for dealing with the documents the app will create.

  3. Even with no visible core data initialization code, Xcode created a .xddatamodeld file and this is the strange part: Xcode named the file Document.xcdatamodeld. Normally Xcode would name that with the same name of the project. By naming it Document it is like saying that this model has something to do with Document.m and Document.h.

Is 1 and 3 a bunch of nonsense from Xcode or am I missing something?

Can you guys explain? Thanks.

Upvotes: 0

Views: 203

Answers (1)

Swift Dev Journal
Swift Dev Journal

Reputation: 20088

Short answer to your questions: the behavior you're seeing is caused by you creating a document-based application. If you created a shoebox (non-document-based) application, you would see different behavior.

Regarding Question 1, when you create a new document, NSPersistentDocument creates a Core Data store and a managed object context for the document. Each document has its own Core Data store and managed object context. Creating the store and managed object context in AppDelegate is fine for a shoebox application because a shoebox application has one set of data for the whole application. But creating a store and managed object context in AppDelegate makes no sense for a document-based application because there can be multiple documents open, each with its own store and managed object context.

Regarding Question 3, the data model has the name of the document because a document-based application creates documents. Suppose you're creating a screenwriting application with a document name of Screenplay. When you choose File > New in the application, you're creating a new screenplay, not a new instance of your application. That's why the data model has the name of the document, not the name of the application.

Upvotes: 1

Related Questions