NSDocument new file logic

2011-06-29 Thread Martin Hewitson
Dear list,

I have an NSPersistentDocument subclass which encapsulates a project for the 
user. When the user creates a new project, they specify a name and a location 
on disk. I then want to create a new instance of my NSDocument subclass as if 
it was saved exactly where the user wants it to be. I want to do this because 
the app then creates additional files (which are managed by the project -- 
think Xcode) which are placed in the same location as the project file. 

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. This was working, but 
it does mean that the user is immediately presented with a save panel, which is 
not so nice. I'd rather present a custom panel telling them to enter a project 
name and a location on disk. Anyway, that aside, this scheme doesn't seem 
robust to future OS implementations (maybe the saveDocument: behaviour changes, 
for example).  What I'd really like is an NSDocumentController method like

-(id)createDocumentAtURL:(NSURL*)aURL ofType:(NSString*)type 
error:(NSError**)anError;

but that doesn't seem to exist. 

Can anyone recommend a way to do what I want? In other words, to 
programmatically create an NSDocument instance already saved on disk? This 
behaviour is essentially what Xcode is doing when one creates a new project.

Best wishes,

Martin


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: martin.hewit...@aei.mpg.de
WWW: http://www.aei.mpg.de/~hewitson






___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSDocument new file logic

2011-06-29 Thread Quincey Morris
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


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSDocument new file logic

2011-06-29 Thread Martin Hewitson
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: martin.hewit...@aei.mpg.de
WWW: http://www.aei.mpg.de/~hewitson






___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com