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 7614463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:24:38+00:00 2026-05-31T02:24:38+00:00

I have an NSPersistentDocument subclass using NSManagedObject subclasses for my data. When a new

  • 0

I have an NSPersistentDocument subclass using NSManagedObject subclasses for my data.

When a new document is opened, I do some initializing of data structures (trivial amount of populating fields). What I’ve noticed is that the Untitled document gets autosaved, and when the application re-opens, that document gets loaded. If the application quits, the user doesn’t (by default) get prompted with the save dialog. If the window closes, the user does.

First question:

I want to call up the save dialog when the user quits the application. I don’t want this Untitled document hanging around (under normal circumstances). I either want it saved or trashed.

I attempted to fill out:

- (void)applicationWillTerminate:(NSNotification *)aNotification

In order to trigger the document to be saved. Calling save: on the context at this point gives an error. From what I can tell, this is because the user hasn’t yet saved the file on their own. In addition, calling [self close]; or [[self windowForSheet] close]; close the window without saving.

How can I force the save dialog to come up? How can I trash the untitled document?

Second question (no, I can’t count):

Since when the application starts, there may or may not be an Untitled document to deal with, I’m trying to keep track of the state in another model. I’ve already found that the initial data (to which I referred earlier) is present when the Untitled document came up. My other model has some metadata, including a success flag/state for the populated data. Once the populated data is all in place and correct, the state indicates as such. Unfortunately, while my populated data is being loaded when the app starts with a pre-existing Untitled document, the metadata class is not.

Please excuse the roughness of the code, at this point, I’m mucking it up until I can see that it’s working how I want before I polish it back off:

- (bool) createGameState {
  NSEntityDescription* description = [NSEntityDescription entityForName:[GameState name] inManagedObjectContext:[self managedObjectContext]];
  NSFetchRequest* req = [[NSFetchRequest alloc] init];
  [req setEntity:description];

  NSError *error = nil;
  NSArray *array = [[self managedObjectContext] executeFetchRequest:req error:&error];
  [req release];
  req = nil;

  GameState* result = nil;
  if (array) {
    NSUInteger count = [array count];
    if (!count) {
      // Create the new GameState.
      DebugLog(@"Creating GameState");
      result = [NSEntityDescription insertNewObjectForEntityForName:[GameState name] inManagedObjectContext:[self managedObjectContext]];
      [result setIsLoaded:[NSNumber numberWithBool:NO]];
    } else {
      if (count > 1) {
        NSLog(@"WARNING: Potentially Corrupt Game State. found: %lu", count);
      }
      result = [array objectAtIndex:0];
      if ([result isLoaded]) {
        [self variantLoaded];
      } else {
        // In this case, we have an aborted set-up. Since the game isn't
        // playable, just refuse to create the GameState. This will
        // force the user to create a new game.
        return NO;
      }
    }
  } else {
    DebugLog(@"error: %@", error);
  } 

  [game setState:result];
  return result;
}

Note that array is always present, and count is always zero. No, I’m not explicitly calling save: anywhere. I’m relying on the standard auto-save, or the user performing a save.

EDIT:

I installed the Core Data Editor app. It turns out the issue isn’t on saving the data, but on loading it. (Note: Due to another issue, the app saves as binary when instructed to save as XML, which causes much head banging.)

I’ve broken it down to the simplest code, which should pick up all objects of type GameState in an array. It retrieves none, despite there clearly being objects of the appropriate type in the saved file:

  NSManagedObjectContext* moc = [self managedObjectContext];
  NSEntityDescription* entity = [NSEntityDescription entityForName:@"GameState" inManagedObjectContext:moc];

  NSFetchRequest* req = [[NSFetchRequest alloc] init];
  [req setEntity:entity];


  NSError *error = nil;
  NSArray *array = [moc executeFetchRequest:req error:&error];

Array is not null, but [array count] is 0.

At this point, I’m guessing it’s something simple that I’m overlooking.

Second EDIT:

I added -com.apple.CoreData.SQLDebug 5 and saved as SQLite. The call to executeFetchRequest does not generate any debug logs. I do see the INSERT INTO ZGAMESTATE entry show up in the logs. It seems that executeFetchRequest is not getting passed to the backend.

Third EDIT (this one burns):

I created a new xcode project, using core data (as I had with the other). I copied just this one function (stubbing where necessary) and plopped a call to it in windowControllerDidLoadNib. In this new project, the code above works.

  • 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-05-31T02:24:39+00:00Added an answer on May 31, 2026 at 2:24 am

    Found the problem.

    I errantly was loading objects in Document‘s - (id) init call. Moved to windowControllerDidLoadNib (which is what I did in the test version) and it worked fine.

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

Sidebar

Related Questions

Have some code: using (var ctx = new testDataContext()) { var options = new
I'm using a NSPersistantDocument backed by Core Data in my application. Each document has
I have document based application. It worked fine until I decided to change data
Have you ever had to justify the choice over using .NET instead of Java
I have a question about Core Data. When starting my appliction, when is my
Have converted devise new session from erb to Haml but doens't work, this is
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Using this guide I have created a static library (let's call it AppCore )
Have some dates in my local Oracle 11g database that are in this format:

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.