Search code examples
iosuikituidocument

UIDocument autosave not working when calling updateChangeCount


I have a custom subclass of UIDocument that I use to store the user's content for my app. I call -[UIDocument updateChangeCount:UIDocumentChangeDone] directly to track changes to the document. Saving and loading work fine, but the document never autosaves. Why would this be happening?


Solution

  • It turns out that the problem was that I wasn't calling -[UIDocument updateChangeCount:] from the main thread. Despite the fact that UIDocument isn't a UI element, it is still part of UIKit and so the usual caveats about always interacting with UIKit classes from the main thread still applies.

    Wrapping the code in a dispatch to the main queue fixed the issue:

    dispatch_async(dispatch_get_main_queue(), ^{
        [doc updateChangeCount:UIDocumentChangeDone];
    });