No DistributedNotificationCenter.postNotificationName(_:object:userInfo:deliverImmediately:) method available?

When I try:

let center = DistributedNotificationCenter.default
center.postNotificationName(Notification.Name(rawValue: "SomeName"), object: "SomeString", deliverImmediately: true)

I get compiler error: Extra argument 'deliverImmediately' in call. What I'm trying to do is post a distributed notification from FileProvider extension. When I check documentation I see that deliverImmediately parameter exists.

Does anyone know what's going on there?

Answered by DTS Engineer in 722815022

This compiles on my machine:

let name = Notification.Name(rawValue: "hello.cruel.world")
DistributedNotificationCenter.default().postNotificationName(name, object: nil, deliverImmediately: true)

I’m using Xcode 13.4.1 in a macOS command-line tool target.

You seem to be missing the parens after the default. This means that in your code center is the type, not an instance, and thus you end up trying to call class methods.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

This compiles on my machine:

let name = Notification.Name(rawValue: "hello.cruel.world")
DistributedNotificationCenter.default().postNotificationName(name, object: nil, deliverImmediately: true)

I’m using Xcode 13.4.1 in a macOS command-line tool target.

You seem to be missing the parens after the default. This means that in your code center is the type, not an instance, and thus you end up trying to call class methods.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you Eskimo! That was the issue. Now sending notification from FileProviderExtension to the Main app works.

Would you recommend using DistributedNotificationCenter for exchainging messages between MainApp and FileProviderExtension or XPC? I find DistributedNotificationCenter so simple to use - just post a notification and receive on other end. But I sense that XPC can be more powerful and more convenient for cases when many messages are sent in short time - ie updating file transfer progress in the Main App UI (FileProviderExtension downloads/uploads a file and wants to update transfer progress to the Main App UI).

Would you recommend using DistributedNotificationCenter for exchainging messages between MainApp and FileProviderExtension or XPC?

It really depends on your goals. If you just want one side to poke the other, notifications are fine. The advantage of XPC is that your message can carry content.

One thing to be careful of with notifications is security. Remember that any process can subscribe to and see your notifications.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

No DistributedNotificationCenter.postNotificationName(_:object:userInfo:deliverImmediately:) method available?
 
 
Q