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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:47:41+00:00 2026-06-02T19:47:41+00:00

I have an NSManagedObject called appointment that I edit the attributes of. If I

  • 0

I have an NSManagedObject called appointment that I edit the attributes of. If I the user presses cancel I want to reverse all of those edits.

If I do (example code)

[[appointment managedObjectContext] setUndoManager:[[NSUndoManager alloc] init]]; //however doing a nslog on undoManager still shows it as (null);
[[[appointment managedObjectContext] undoManager] beginUndoGrouping];
appointment.startTime = 11;
appointment.endTime = 12;
appointment.customer = @"Tom";
[[[appointment managedObjectContext] undoManager] endUndoGrouping];
[[[appointment managedObjectContext] undoManager] undo];

shouldn’t it undo all change changes in between beginUndoGrouping and endUndoGrouping? It seems there are numerous ways to do this but I cannot seem to find the correct way. What is the correct way to undo changes on an NSManagedObject?

  • 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-02T19:47:42+00:00Added an answer on June 2, 2026 at 7:47 pm

    I imagine that is just an example of the order in which events would proceed, and not an actual example.

    Did you, by chance, forget to give the ManagedObjectContext a NSUndoManager?

    I believe you get one by default under OS X, but under iOS, you have to specifically provide one.

    You want to be sure to set the undo manager when you create your MOC…

    managedObjectContext.undoManager = [[NSUndoManager alloc] init];
    

    If the undo-manager is nil, after doing this, then you are using multiple MOCs, or some other code has reset it.

    Also, for the purpose of debugging, check the appointment.managedObjectContext property, and make sure it is not nil and references a valid MOC.

    EDIT

    Ok, I just went and wrote a quick test, using a simple model. Maybe you should do something similar to see where your assertions are failing (you can just add normal assert in your code path – I did this one as a unit test so I could easily add it to an existing project).

    - (void)testUndoManager
    {
        NSDate *now = [NSDate date];
        NSManagedObjectContext *moc = [self managedObjectContextWithConcurrencyType:NSConfinementConcurrencyType];
        STAssertNil(moc.undoManager, @"undoManager is nil by default in iOS");
        moc.undoManager = [[NSUndoManager alloc] init];
        [moc.undoManager beginUndoGrouping];
        NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:EVENT_ENTITY_NAME inManagedObjectContext:moc];
        STAssertNotNil(moc, @"Managed Object is nil");
        STAssertEquals(moc, object.managedObjectContext,  @"MOC of object should be same as MOC");
        STAssertNotNil(object.managedObjectContext.undoManager, @"undoManager of MOC should not be nil");
        [object setValue:now forKey:@"timestamp"];
        STAssertEqualObjects(now, [object valueForKey:@"timestamp"], @"Timestamp should be NOW");
        [moc.undoManager endUndoGrouping];
        STAssertEqualObjects(now, [object valueForKey:@"timestamp"], @"Timestamp should be NOW");
        [moc.undoManager undo];
        STAssertNil([object valueForKey:@"timestamp"], @"Object access should be nil because changes were undone");
    }
    

    EDIT

    The MOC of a managed object can be set to nil under several conditions. For example, if you delete an object, and then save the mod, the MOC will be set to nil for that object…

    NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"SomeEntity" inManagedObjectContext:moc];
    [object.managedObjectContext deleteObject:object];
    [moc save:0];
    // object.managedObjectContext will be nil
    

    Another, less common case, but a sign that there may be a memory issue with the MOC… Under ARC, the MOC of a managed object is a weak pointer. Thus, if the MOC goes away, that pointer will be reset to nil. Under non-ARC, the pointer will just have the old value, and your results will be undefined… probably a crash.

    So, if managedObject.managedObjectManager is nil, the most likely culprits are:

    1. The object was never inserted into a MOC
    2. The object was deleted from a MOC
    3. The MOC was deleted
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an NSManagedObject with attributes that a user can edit with a view.
I have created an NSManagedObject called TItem. To that object, I've added a helper
I have method that returned NSManagedObject and I don't know what kind of NSManagedObject
I have a Data class that subclasses NSManagedObject with a single bool property attribute.
I have a ConfiguracaoDaApp class in my project that is a NSManagedObject subclass. I
I have a CoreData NSManagedObject subclass, TextNarration, that has an attribute of type NSString,
Say I have an NSManagedObject subclass called Item . Whenever an item instance is
I have a CoreData model (managed object) called Item: @interface Item : NSManagedObject {
I have an entity class called catObras, and it inherits from the class NSManagedObject.
I have a NSManagedObject object filled with data I want to use in multiple

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.