NSPersistentCloudKitContainer - notification

I would like to implement notifications about NSPersistentCloudKitContainer

I find this classe but I receive no notifications in my app!?!

Do I need more settings to use it?

thanx for your help

import Combine
import CoreData

@available(iOS 14.0, *)
class SyncMonitor {
    /// Where we store Combine cancellables for publishers we're listening to, e.g. NSPersistentCloudKitContainer's notifications.
    fileprivate var disposables = Set<AnyCancellable>()

    init() {
        NotificationCenter.default.publisher(for: NSPersistentCloudKitContainer.eventChangedNotification)
            .sink(receiveValue: { notification in
                if let cloudEvent = notification.userInfo?[NSPersistentCloudKitContainer.eventNotificationUserInfoKey]
                    as? NSPersistentCloudKitContainer.Event {
                    // NSPersistentCloudKitContainer sends a notification when an event starts, and another when it
                    // ends. If it has an endDate, it means the event finished.
                    if cloudEvent.endDate == nil {
                        print("Starting an event...") // You could check the type, but I'm trying to keep this brief.
                    } else {
                        switch cloudEvent.type {
                        case .setup:
                            print("Setup finished!")
                        case .import:
                            print("An import finished!")
                        case .export:
                            print("An export finished!")
                        @unknown default:
                            assertionFailure("NSPersistentCloudKitContainer added a new event type.")
                        }

                        if cloudEvent.succeeded {
                            print("And it succeeded!")
                        } else {
                            print("But it failed!")
                        }

                        if let error = cloudEvent.error {
                            print("Error: \(error.localizedDescription)")
                        }
                    }
                }
            })
            .store(in: &disposables)
    }
}

Hello,

Are you using a simulator? I tried your code in TCA environment, and I am receiving messages. iPhone 11, wifi connected, and iCloud signed in. If using a real device, I would check if the container is pointing towards the correct iCloud container.

            case .subscribeCloudEvent:
                  return NotificationCenter.default.publisher(for: NSPersistentCloudKitContainer.eventChangedNotification)
                       .compactMap { $0.userInfo?[NSPersistentCloudKitContainer.eventNotificationUserInfoKey] as? NSPersistentCloudKitContainer.Event }
                      .receive(on: env.mainQueue)
                      .eraseToEffect()
                      .map(Action.cloudEventResult)
                      .cancellable(id: cloudEventID())
NSPersistentCloudKitContainer - notification
 
 
Q