3

Is there any way to disable auto-save for UIManagedDocument ?

I present NSManagedObjects in a controller where the user can add and delete them. But I just want to save those changes when the user explicitly fires a save action. Otherwise I want to discard the changes.

Thanks for your help!

kahlo
  • 2,314
  • 3
  • 28
  • 37
  • what happen if your app crashed/get killed and user have no change to press save button? – Bryan Chen Nov 12 '13 at 21:19
  • That is a good point. For my situation though it is good to revert to the previous state. Thanks for the remark! – kahlo Nov 12 '13 at 22:40

2 Answers2

3

Can't you override the method below in the UIManagedDocument subclass

- (void)autosaveWithCompletionHandler:(void (^)(BOOL success))completionHandler

EDIT: Here are some additional methods you might want to include. I use the first one to confirm if and when auto-saves were happening and the second to debug certain errors, the details of which can't be obtained any other way. That's all thats in my subclass so its pretty trivial to add this.

@implementation YourManagedDocument


- (id)contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError
{
    NSLog(@"Auto-Saving Document");
    return [super contentsForType:typeName error:outError];
}

- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted
{
    NSLog(@" error: %@", error.localizedDescription);
    NSArray* errors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
    if(errors != nil && errors.count > 0) {
        for (NSError *error in errors) {
            NSLog(@" Error: %@", error.userInfo);
        }
    } else {
        NSLog(@" error.userInfo = %@", error.userInfo);
    }
}
@end
Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76
  • Yes, it seems to me this is what I will end up doing and passing a flag in the controller I want to disable auto-save. I was hoping though to find some solution that avoided subclassing. Thanks anyway! – kahlo Nov 18 '13 at 22:54
0

See this SO answer for the details, but unless you explicitly save your NSManagedObjectContext, you can call [managedObjectContext rollback] to undo any changes the user made.

Community
  • 1
  • 1
paulrehkugler
  • 3,241
  • 24
  • 45
  • That seems the correct approach. However, how does it know until which point to discard changes? The auto-saving happens on an unpredictable way. Thanks for your reply! – kahlo Nov 12 '13 at 22:57