Search code examples
ioscore-datauimanageddocument

Disable auto-saving in UIManagedDocument


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!


Solution

  • 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