• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSDocument new file logic
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDocument new file logic


  • Subject: Re: NSDocument new file logic
  • From: Martin Hewitson <email@hidden>
  • Date: Wed, 29 Jun 2011 10:14:16 +0200

Wonderful! Thanks very much. I will give this a try. Writing out the core data store was the bit I was missing.

Martin


On Jun 29, 2011, at 9:54 AM, Quincey Morris wrote:

> On Jun 28, 2011, at 23:58, Martin Hewitson wrote:
>
>> In the past I achieved this in a somewhat unsatisfactory way by just calling newDocument: then saveDocument: and getting the user to immediately save the document before the app does the rest of the setup steps.
>
> Using the action methods (newDocument: and saveDocument:) is what makes your approach unappealing for the user. In Snow Leopard or earlier, for a regular document, the steps are something like this:
>
> 1. Get the project name and file system location. (You can use the Save panel for this, but you put it up yourself rather than having 'newDocument:' do it. Or, you can use some kind of custom dialog.)
>
> 2. Create a document file at that location. (Typically, you create a default data model or import some data, turn it into a keyed archive, write the archive data to a file.)
>
> 3. Use NSDocumentController's 'openDocumentWithContentsOfURL:display:error:' method to open the now-existing document normally.
>
> You can use much the same approach for NSPersistentDocument, but step 2 is a little different. Here's a method I wrote (based on some sample code somewhere in the Core Data documentation, but I don't remember where) that creates a new store. It returns a managed object context because you probably want to put something in the store (and 'save:' it) before re-opening it as a NSPersistentDocument in step 3.
>
>> + (NSManagedObjectContext*) managedObjectContextForStoreURL: (NSURL*) storeURL
>> {
>> 	//	Find the document's model
>>
>> 	NSManagedObjectModel* model = [NSManagedObjectModel mergedModelFromBundles: nil];
>> 	if (!model)
>> 		return nil;
>>
>> 	//	Create a persistent store
>>
>> 	NSPersistentStoreCoordinator* psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: model];
>> 	if (!psc)
>> 		return nil;
>>
>> 	NSError* error;
>> 	NSPersistentStore* store = [psc addPersistentStoreWithType: NSSQLiteStoreType
>> 												 configuration: nil
>> 														   URL: storeURL
>> 													   options: nil
>> 														 error: &error];
>> 	if (!store)
>> 		return nil;
>>
>> 	//	Create a managed object context for the store
>>
>> 	NSManagedObjectContext* managedContext = [[NSManagedObjectContext alloc] init];
>> 	if (!managedContext)
>> 		return nil;
>>
>> 	managedContext.persistentStoreCoordinator = psc;
>> 	managedContext.undoManager = nil;
>>
>> 	return managedContext;
>> }
>
> HTH
>
>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Martin Hewitson
Albert-Einstein-Institut
Max-Planck-Institut fuer
    Gravitationsphysik und Universitaet Hannover
Callinstr. 38, 30167 Hannover, Germany
Tel: +49-511-762-17121, Fax: +49-511-762-5861
E-Mail: email@hidden
WWW: http://www.aei.mpg.de/~hewitson
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >NSDocument new file logic (From: Martin Hewitson <email@hidden>)
 >Re: NSDocument new file logic (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: problem with applying md5 to data
  • Next by Date: Properties, Attributes and Retain+Autorelease
  • Previous by thread: Re: NSDocument new file logic
  • Next by thread: Properties, Attributes and Retain+Autorelease
  • Index(es):
    • Date
    • Thread