문제

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.

올바른 솔루션이 없습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top