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

  • Home
  • SEARCH
  • 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 9203933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:35:11+00:00 2026-06-17T23:35:11+00:00

I have a Cocoa document-based app and running into some problems with NSUndoManager even

  • 0

I have a Cocoa document-based app and running into some problems with NSUndoManager even though I’m having Core Data take care of all of it.

So every time a new persistent document is created I create a root context and a child context. The root context is responsible for writing to the persistent document and the child context is the context that I modify via create/edit/delete NSManagedObjects. I set the root context as the parent context. I also set the undo manager of the child context to the original context that was created with the NSPersistentDocument. Here’s some code to hopefully make it a bit clearer:

// create root context
NSManagedObjectContext *rootContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];

// startContext is the context created with the document
[rootContext setPersistentStoreCoordinator:[startContext persistentStoreCoordinator]];
NSManagedObjectContext *childContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];

// set root as parent
[childContext setParentContext:rootContext];

// set undo
[childContext setUndoManager:[startContext undoManager]];

The reason I do all of this is because I ran into a similar problem as described here:
Enable saving of document NSManagedObjectContext immediately?

So I bring this up because this is the only code in my application where I even touch the NSUndoManager. I am testing my app by simply inserting NSManagedObjects and then Undo-ing the insertion. Sometimes after two undos, or maybe five, or maybe even ten I receive the following error:

_endUndoGroupRemovingIfEmpty:: NSUndoManager 0x100159f30 is in invalid state,   endUndoGrouping called with no matching begin
2013-01-29 21:31:23.375 TestApplication[30125:303] (
0   CoreFoundation                      0x00007fff8a54f0a6 __exceptionPreprocess + 198
1   libobjc.A.dylib                     0x00007fff8215f3f0 objc_exception_throw + 43
2   CoreFoundation                      0x00007fff8a54ee7c +[NSException raise:format:] + 204
3   Foundation                          0x00007fff80ea021f -[NSUndoManager _endUndoGroupRemovingIfEmpty:] + 195
4   Foundation                          0x00007fff80ea0154 -[NSUndoManager endUndoGrouping] + 42
5   Foundation                          0x00007fff80ed79da +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 447
6   AppKit                              0x00007fff8253632d -[NSApplication run] + 687
7   AppKit                              0x00007fff824dacb6 NSApplicationMain + 869
8   TestApplication                     0x0000000100001512 main + 34
9   TestApplication                     0x00000001000014e4 start + 52

So if I’m reading the debugging information correctly then I need to call [[context undoManager] beginUndoGrouping] however the problem with this is that no where in my program do i use the `[[context undoManager] endUndoGrouping]’. Has anyone experience this before?

Any help is appreciated.

  • 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-17T23:35:13+00:00Added an answer on June 17, 2026 at 11:35 pm

    Each context in OSX creates an undoManager by default (nil in iOS). The reason for the grouping error was that your child context undoManager was trying to nest changes inside the rootContext’s undoManager which was nested inside the startContext undoManager (which was the same as the child context’s undoManager).

    // create root context
    NSManagedObjectContext *rootContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    
    // startContext is the context created with the document
    [rootContext setPersistentStoreCoordinator:[startContext persistentStoreCoordinator]];
    [rootContext setUndoManager:startContext.undoManager];
    NSManagedObjectContext *childContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    
    // set root as parent
    [childContext setParentContext:rootContext];
    
    • 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 have a document-based Cocoa app using CoreData which can import large amounts of
I have a document-based Cocoa app. During runtime, I load an additional nib from
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
My document-based Cocoa application uses a NSOutlineView/NSTreeController combo, bound to the document's Core Data
I have a Cocoa, Document-based Mac OS X application. One feature that I have
Assume I have a Cocoa-based Mac or iOS app. I'd like to run a
I've a cocoa app based on NSDocument, I've two types of document. Every document
I am working on a Cocoa document based application. I have a menu item

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.