1

Not overriding autosaveWithCompletionHandler:, whenever the document is changed( [doc updateChangeCount: UIDocumentChangeDone]) autosaveWithCompletionHandler: is periodically called.

But if I override this method, it is called only once.

Document has been changed -> Time is passing... -> Overrided method has been called -> Document has been changed -> Time is passing... -> Time is passing... -> Document has been changed -> Time is passing... -> Time is passing...

I make the document change by calling [doc updateChangeCount: UIDocumentChangeDone].

(overriding method)

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

    if ([self hasUnsavedChanges]) {
        [self saveToURL: self.fileURL forSaveOperation: UIDocumentSaveForOverwriting completionHandler: ^(BOOL success) {
            if (success) {
                NSLog(@"%@ has been autosaved", [self description]);
                completionHandler(YES);
            }
            else {
                NSLog(@"Failed to autosave %@", [self description]);
                completionHandler(NO);
            }
        }];
    }
}   // autosaveWithCompletionHandler:

Thank you for your reading.

Seong
  • 33
  • 1
  • 4

1 Answers1

0

You shouldn't be overriding saveWithCompletionHandler: or autosaveWithCompletionHandler:; those methods make changes to private properties which help the system to deterine whether the object needs saving, and when you override the methods those changes don't get made. Instead, you should be overriding contentsForType:error:.

Simon
  • 25,468
  • 44
  • 152
  • 266
  • I have begun working on someone else's code that does override this... - (void)autosaveWithCompletionHandler:(void (^)(BOOL success))completionHandler { [super autosaveWithCompletionHandler:completionHandler]; [[NSNotificationCenter defaultCenter] postNotificationName:DOCUMENT_AUTOSAVE_OPERATION object:self]; } why would this be a problem, and how else should I address the need for notification if I were to not override – Bradley Thomas Nov 24 '13 at 19:24
  • That's probably OK, though I'm not sure why you'd need that notification. – Simon Nov 25 '13 at 08:53
  • Thanks. The reason looks like it calls a method which NSLogs the save, which in turn calls another method to do a repeat save. And that method issues *another* (different kind) of save notification. :/ – Bradley Thomas Nov 25 '13 at 15:33