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 ([email protected])
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 [email protected]