Undo Management with Core Data

前端 未结 1 1600
不思量自难忘°
不思量自难忘° 2021-02-04 09:02

I\'m trying to implement undo support using Core Data on the iPhone and I ran into a few problems.

I currently have a couple of managed objects set up but when I make ch

相关标签:
1条回答
  • 2021-02-04 09:19

    While the undo features will work pretty much out of the box, you do need to allocate an NSUndoManager for the NSManagedObjectContext for which you want undo support.

    The easiest way to do this is to set up the undo support when something asks your appDelegate for the NSManagedObjectContext

    This is the default method that apple gives you:

    - (NSManagedObjectContext *) managedObjectContext {
    
        if (managedObjectContext != nil) {
            return managedObjectContext;
        }
    
        NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
        if (coordinator != nil) {
            managedObjectContext = [[NSManagedObjectContext alloc] init];
    
            [managedObjectContext setPersistentStoreCoordinator: coordinator];
        }
        return managedObjectContext;
    }
    

    Modify it to look like this:

    - (NSManagedObjectContext *) managedObjectContext {
    
        if (managedObjectContext != nil) {
            return managedObjectContext;
        }
    
        NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
        if (coordinator != nil) {
            managedObjectContext = [[NSManagedObjectContext alloc] init];
    
            //Undo Support
            NSUndoManager *anUndoManager = [[NSUndoManager  alloc] init];
            [managedObjectContext setUndoManager:anUndoManager];
            [anUndoManager release];
    
            [managedObjectContext setPersistentStoreCoordinator: coordinator];
        }
        return managedObjectContext;
    }
    
    0 讨论(0)
提交回复
热议问题