2

I'm trying to test some code with an undo manager, but apparently, undo's get grouped when run in the same run loop. I have tried turning off groupsByEvent, but it doesn't work as expected.

- (NSString *)article
{
    return _article == nil ? @"no article" : _article;
}
- (void)setarticle:(NSString *)article
{

    if (_undoManager)
    {
        [(ModelGraph *)[_undoManager prepareWithInvocationTarget:self] setarticle:self.article];
        [_undoManager setActionarticle:@"undo set article"];
    }

    NSCharacterSet *charactersToRemove = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
    article = [[article componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString:@""];

    _article = article;
}

and in my test I have:

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

[myObject setArticle:@"Article 1"];
[myObject setArticle:@"Article 2"];
[myObject setArticle:@"Article 3"];
[myObject setArticle:@"Article 4"];
[undoManager undo];

NSLog(@"myObject.article: %@", [myObject article]); //prints noarticle

But when the code runs, I always get back: "no article" - as when the undo happens, it always runs all the undo's back...because they were added in the same runloop.

I just want to test that my undo is working with a test...and ideas?

Maybe force the run loop to fire after each change? Change a run loop mode?

Any help welcome.

Thanks, Chris

Chris
  • 2,739
  • 4
  • 29
  • 57

1 Answers1

0

ah, this seems to work, doesn't look the nicest though.

[undoManager setGroupsByEvent:NO];
[undoManager beginUndoGrouping];

dispatch_async (dispatch_get_main_queue(), ^{


    [myObject setArticle:@"Article 1"];

    [undoManager endUndoGrouping];

    [undoManager beginUndoGrouping];


    dispatch_async (dispatch_get_main_queue(), ^{
        [myObject setArticle:@"Article 2"];

        [undoManager endUndoGrouping];

        [undoManager undo];
        NSLog(@"myObject.article: %@", [myObject article]);
    });
});
Chris
  • 2,739
  • 4
  • 29
  • 57