문제

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!

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top