4

I have an OSX app which uses NSDocument and autosaves inplace - all works well. Sometimes the user will save the document into a Dropbox synced folder and there is then the chance that the document will be updated (via Dropbox) whilst its still open in the app. Question is, how do I detect this and reload the document from disk (or at least inform the user.)

Thanks

Scotty
  • 2,019
  • 1
  • 20
  • 31
  • I haven't used `NSDocument` much so there might be a built-in method for this, however if not I'd suggest using a GCD file descriptor to monitor the file for changes. – sudo rm -rf Dec 14 '13 at 18:46
  • FSEvents is the API used to watch for file changes. – Grady Player Dec 14 '13 at 21:16
  • @GradyPlayer: FSEvents is the API used to watch for *directory* changes. You'd then have to find which file(s) changed yourself. Using kevent (wrapped in CFFileDescriptor or possibly GCD) would enable you to monitor a single file. The downside is, you'd be subject to the file descriptor limit—if the user opened, say, 200 files, you wouldn't be able to watch all of them and could start throwing errors. – Peter Hosey Dec 14 '13 at 21:28
  • @PeterHosey correct, it is for watching directories specifically but the events come in per file and each instance of NSDocument only needs to watch one directory, and ignore all events for the files that it isn't... – Grady Player Dec 14 '13 at 21:52

1 Answers1

8

NSDocument implements the NSFilePresenter protocol out of the box. You can override presentedItemDidChange to update automatically when the file has changed.

David Beck
  • 10,099
  • 5
  • 51
  • 88