Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9230611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:52:09+00:00 2026-06-18T05:52:09+00:00

I have a document-based Cocoa app using CoreData which can import large amounts of

  • 0

I have a document-based Cocoa app using CoreData which can import large amounts of data. Since this import takes some time I do it in the background, but since NSManagedObjectContext is not thread save I used the persistentStoreCoordinator of the managedObjectContext of the document to create a new NSManagedObjectContext in the background thread.
Now, when the import is finished I save the background managedObjectContext to notify the document’s main-thread managedObjectContext of the changes and merge them. As far as I understand it, this is how one is supposed to do concurrency using CoreData.

But sometimes, the persistentStoreCoordinator does not have a persistentStore when I call [managedObjectContex save:] which causes the save to fail and the app to crash. I’m using autosave and all (basically a mostly unmodifed NSPersistentDocument on OS X 10.8) so I assumed that I don’t have to care about how it is saved and it would “just work”.

Apparently, this is not the case.
I tried forcing an autosave operation, hoping that this would create a persistentStore by calling [self autosaveDocumentWithDelegate:self didAutosaveSelector:@selector(document:didAutosave:contextInfo:) contextInfo:nil]; at the end of windowControllerDidLoadNib: in my document subclass, but this doesn’t seem to change anything.
The delegate callback (- (void)document:(NSDocument *)document didAutosave:(BOOL)didAutosaveSuccessfully contextInfo:(void *)contextInfo) actually states the autosave was successful although neither any of the fileURL or related accessors returns something non-nil nor a persistentStore has been created.

I also thought about calling -(BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)url ofType:(NSString *)fileType modelConfiguration:(NSString *)configuration storeOptions:(NSDictionary *)storeOptions error:(NSError *__autoreleasing *)error myself, but I don’t know what URL to use to make it behave just like a normal autosave.
I checked backupFileURL, fileURL and autosavedContentsFileURL but all of them are still nil at the end of windowControllerDidLoadNib:. As a matter of fact, they are even nil after writeToURL:... and configurePersistentStoreCoordinatorForURL:... have been called, so I don’t have a clue how to get a “correct” URL.

To better understand how and when a persistentStore is created I have set breakpoints in writeToURL:... and configurePersistentStoreCoordinatorForURL:.... I noticed that configurePersistentStoreCoordinatorForURL: is called when the application looses frontmost status. But when I just create a new document and try importing something right after it was created (without first switching to another app to cause the persistentStore to be created) it crashes. If I switch to another app, the store is created and everything works fine.
By the way, the URL used for the persistentStore created this way is always somewhere in the temp directory.

Am I doing something wrong? Don’t I have to call [managedObjectContext save:] to notify the other managedObjectContext of the changes?
How can I force the document to create its temporary persistentStore? Why doesn’t the call to autosaveDocumentWithDelegate:... actually save the document (and thus create a persistentStore)?

Related:

  • New project New Model NSPersistentDocument This NSPersistentStoreCoordinator has no persistent stores Similar problem but the solution there was to not save the context or present an error. I think I have to save the context to notify the second one of the changes and don’t want to force the user to save the file manually (after all, that’s what autosave is for).
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:52:10+00:00Added an answer on June 18, 2026 at 5:52 am

    I solved a similar problem: I need to generate permanent Object IDs, which require the presence of a persistent store. Like you point out, untitled documents that haven’t yet been autosaved don’t have persistent stores.

    This abbreviated snippet is from my NSWindowController subclass:

    - (void) awakeFromNib {
        //hacky way to get an autosave to generate an NSPersistentStore.
        GSNativeDocument *doc = self.document;
        [doc updateChangeCount:NSChangeDone];
        [doc autosaveDocumentWithDelegate:self didAutosaveSelector:@selector(document:didAutosave:contextInfo:) contextInfo:nil];
    }
    
    //called by the autosave operation started in awakeFromNib.
    - (void)document:(NSDocument *)document didAutosave:(BOOL)didAutosaveSuccessfully contextInfo:(void *)contextInfo
    {
        GSNativeDocument *doc = self.document;
        [doc updateChangeCount:NSChangeUndone];
    }
    

    Why does updating the change count create a “real” autosave, when every other way to induce an autosave doesn’t? I have no clue. I’ve filed this bug with Apple in early January and posted in the dev forums, but they have not answered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Environment: xcode 3.2.1, document-based core-data application. I have a document-based cocoa app which uses
I've got a document based App using Core Data, and have a tableView which
I have a document-based Core Data app. My main Core Data entity has several
I have a Cocoa document-based app and running into some problems with NSUndoManager even
I have a document-based Cocoa app. During runtime, I load an additional nib from
I have a Document based core data app. The main document window has a
I have a Mac document-based app, using NSPersistentDocument for the document model. When new
I have a simple Document based Core Data app (built around the standard Apple
I have a simple document-based Cocoa app that acts as a viewer for .wav
I have a Cocoa document-based app that (currently at least) functions as a basic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.