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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:34:18+00:00 2026-05-26T19:34:18+00:00

I have a problem with NSUndoManager not undoing when working with a complex model.

  • 0

I have a problem with NSUndoManager not undoing when working with a complex model.

This is my model.

core data model

I have a Singleton that takes care of the core data stuff, this is its initialization:

        model =[NSManagedObjectModel mergedModelFromBundles:nil];

        NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

        NSString *path = pathInDocumentDirectory(@"store.data");
        NSURL *storeURL = [NSURL fileURLWithPath:path];

        NSError *error = nil;

        if(![psc addPersistentStoreWithType:NSSQLiteStoreType
                              configuration:nil
                                        URL:storeURL 
                                    options:nil 
                                      error:&error]) {
            [NSException raise:@"Open failed" format:@"Reason: %@", [error localizedDescription]];
        }

        context = [[NSManagedObjectContext alloc] init];

        NSUndoManager *contextUndoManager = [[NSUndoManager alloc] init];
        [contextUndoManager setLevelsOfUndo:20];    
        [context setUndoManager:contextUndoManager];

        [context setPersistentStoreCoordinator:psc];

For every entity I have an init method that calls [self initWithEntity…] and then inits some properties. This is an example of the HalfEdge entity:

- (id) initWithVertex:(Vertex*) vert inManagedObjectContext: context {

    NSEntityDescription* tEntityDescription = [NSEntityDescription entityForName: @"HalfEdge"
                                                          inManagedObjectContext: context];

    self = [self initWithEntity: tEntityDescription insertIntoManagedObjectContext: context];       
    if(self) {
        self.lastVertex = vert;
        [self.lastVertex addHalfEdgeObject:self];
    }

    return self;
}

When the user adds a new drawing I create a new Drawing entity and then let the user add points taping the screen. For every point a rutine will get executed that may add and/or remove Triangle entities, halfedges and vertex. This is the call:

[[[DrawingsStore sharedStore].managedObjectContext undoManager] beginUndoGrouping];

[delaunay addPoint:CGPointMake(localizacion.x-dummy.bounds.size.width/2, localizacion.y-dummy.bounds.size.height/2)];

[[DrawingsStore sharedStore].managedObjectContext processPendingChanges];
[[[DrawingsStore sharedStore].managedObjectContext undoManager] endUndoGrouping];

As you can see, I set an undo group for everything that happens inside that rutine.

Then when a button is pressed I’m calling [[context undoManager] undo]; but it does nothing.

I print a fetch before and after the undo and it’s the same. I can see the rutine is working properly, adding all the correct entities to core data, but then it wont undo anything at all.

EDIT with sugestions from Aderstedt

Ok, I deleted the custom init method for NSManagedObject subclasses and created a class method like this:

+ (HalfEdge*) addWithVertex:(Vertex*) vert inManagedObjectContext: context {

    HalfEdge* halfEdge = [NSEntityDescription insertNewObjectForEntityForName:@"HalfEdge" inManagedObjectContext:context];

    if(halfEdge) {
        halfEdge.lastVertex = vert;
        [halfEdge.lastVertex addHalfEdgeObject:self];
    }

    return halfEdge;
}

And still the same result. Objects get created, undo doesn’t work. (canUndo returns 1)

EDIT

Wow, I just registered for NSUndoManagerCheckpointNotification of undoManager and once I click undo it gets posted forever like in a loop. Ok, now I know I must be doing something wrong somewhere, but… where?

  • 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-26T19:34:18+00:00Added an answer on May 26, 2026 at 7:34 pm

    Ok I found out. Turns out I was looking in the wrong place.

    Trying to Debug NSUndoManager I registered for notifications and found out that NSUndoManagerCheckpointNotification was getting called over and over again.

    [delaunay addPoint…] makes all the changes to the model. But at the same time there is a render routine running that renders the triangles to the screen. In that routine I set the color of those triangles. I need to do it there because I don’t know the color I should put before I render the background of the screen.

    Those changes to the color attribute of the NSManagedObject subclass Triangle were causing the NSUndoManagerCheckpointNotification to be fired and the undo to not work. If I remove that, undo works.

    So I figured I just have to add this, so the changes made during render don’t make it to the undo stack.

    [[[DibujosStore sharedStore] managedObjectContext] processPendingChanges];
    [[[[DibujosStore sharedStore] managedObjectContext] undoManager] disableUndoRegistration];
    [renderer render];
    [[[DibujosStore sharedStore] managedObjectContext] processPendingChanges];         
    [[[[DibujosStore sharedStore] managedObjectContext] undoManager] enableUndoRegistration];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have problem while getting data from Memcached on .NET MVC solution. I have this
I have problem with pointers. This is working fine - int main(void){ char *w;
I have problem in some JavaScript that I am writing where the Switch statement
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem when I try insert some data to Informix TEXT column via
I have problem with fancybox. I want to write a function that will run
I have problem when asking for default ILogger from Unity container. I have this
I have problem with using dequeueReusableCellWithIdentifier method. Whenever I use this one, one cell
I have problem with reading buffer, when reading is not completed. If the received
I have problem with encoding data in Oracle database. I want to xor string

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.