Questions tagged [nsfilepresenter]

The NSFilePresenter protocol should be implemented by objects that allow the user to view or edit the content of files or directories.

The NSFilePresenter protocol should be implemented by objects that allow the user to view or edit the content of files or directories.

You use file presenters in conjunction with an NSFileCoordinator object to coordinate access to a file or directory among the objects of your application and between your application and other processes. When changes to an item occur, the system notifies objects that adopt this protocol and gives them a chance to respond appropriately.

You use the methods of this protocol to respond to actions about to be taken on the presented file or directory. When another object or process uses a file coordinator to begin reading or writing a file or directory, the file coordinator notifies all presented objects interested in the item first. It notifies the presenter objects by invoking one of the methods defined by this protocol on that object. The actual invocation of that method occurs on the operation queue in the presentedItemOperationQueue property. Your file presenter must provide this queue. If your queue supports the concurrent execution of operations, the methods of your presenter object must be thread-safe and able to run in multiple queues simultaneously.

You can use file presenters to coordinate access to a file or directory among your application’s objects. If another process uses a file coordinator for the same file or directory, your presenter objects are similarly notified whenever the other process makes its changes. Your presenter objects are not notified about changes made directly using low-level read and write calls to the file. Only changes that go through a file coordinator result in notifications.

Reference: https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSFilePresenter_protocol/Reference/Reference.html

9 questions
20
votes
2 answers

Swift / Cocoa: How to watch folder for changes?

I'm writing a small macOS app, where I want to be able to watch a folder for changes. It doesn't need to watch subfolder, I only want to receive a notification if a file is added to the folder or removed. It looks like NSFileCoordinator and/or…
eivindml
  • 2,197
  • 7
  • 36
  • 68
9
votes
2 answers

Long delay with NSFileCoordinator coordinateWritingItemAtURL

I'm setting up NSFileCoordinator and NSFilePresenter in my app so I can do file IO from my AppleWatch app safely. There are some places in my code where I write to a file a couple of times in quick succession. This is a problem in and of itself and…
Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
7
votes
0 answers

Track external file content changes

I've fully implemented the NSFilePresenter protocol using NSFileCoordinator to track external changes made to an imported folder/file tree. And it generally works. However, I'm still a bit confused. I've implemented almost all (if not all) of the…
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
6
votes
3 answers

Why do NSFilePresenter protocol methods never get called?

I am trying to monitor file changes in local and iCloud directories and have implemented the NSFilePresenter protocol methods but the only method that gets called is presentedItemAtURL. Am I correct in assuming that I should be able to monitor a…
Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76
4
votes
1 answer

iOS 14: NSFilePresenter not getting callbacks/notifications; works on iOS13

I have an UIViewController that adopts the NSFilePresenter protocol. I have implemented presentedItemDidChange() in order to receive changes of files stored in an ubiquitous container on iCloud. My implementation is working OK on iOS13.7, but fails…
Chris
  • 1,231
  • 2
  • 10
  • 20
2
votes
0 answers

Correct usage of NSFileCoordinator in Swift3

I'm trying to run my program as an sandboxed app. It's a Drag&Drop PDF Converter. Everything works fine but creating & converting the file doesn't work well since i turned it into a sandboxed app. What i tried to do with NSFileCoordinator is: let…
qalt
  • 29
  • 2
1
vote
1 answer

How to make NSFileCoordinator generate an error in a unit test environment?

I wrote an async interface for the NSFileCoordinator API. struct FileCoordinator { private init() {} static let shared = FileCoordinator() func coordinateWriting(of data: Data, to url: URL) async throws { try await…
1
vote
1 answer

When initialising an NSFilePresenter for a fileURL within an NSFileCoordinator accessor block passing newURL, which URL should I use?

Apple's documentation is not as clear as I would like when it comes to understanding subtle details relating to NSFileCoordinator usage. My current interpretation is as follows: When you access a fileURL using NSFileCoordinator, the coordinator…
1
vote
1 answer

Failed attempt using Related Items to create backup file in sandboxed app

The App Sandbox design guide says: The related items feature of App Sandbox lets your app access files that have the same name as a user-chosen file, but a different extension. This feature consists of two parts: a list of related extensions…