Not receiving Persistent Store Remote Change Notification.

Before the code...

  1. CloudKit, background mode (remote notifications) and push notifications are added to Capabilities.
  2. Background sync is working fine and view loads current store on manual fetch.

Code that initialises the persistent container in app delegate...

- (NSPersistentCloudKitContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentCloudKitContainer alloc] initWithName:@"Expenses"];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                    
                    __block NSPersistentStoreDescription *sDescription = storeDescription;
                    dispatch_async(dispatch_get_main_queue(), ^(){
                        [sDescription setOption:[NSNumber numberWithBool:YES] forKey:@"PersistentHistoryTracking"];
                        [sDescription setOption:[NSNumber numberWithBool:YES] forKey:@"NSPersistentStoreRemoteChangeNotificationOptionKey"];
                    });
#ifdef DEBUG
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
#endif
                    abort();
                }
                else
#ifdef DEBUG
                    NSLog(@"Store successfully initialized");
#endif
            }];
        }
    }
    return _persistentContainer;
}

In Home view controller which is the initial view controller i am adding an observer for the remote notification...

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadViewONCKChangeNotification) name:NSPersistentStoreRemoteChangeNotification object:[appDelegate.persistentContainer persistentStoreCoordinator]];

I am not receiving any store change notifications. Have added breakpoints to see if selector is fired. no go. CloudKit Console also doesn't show any pushes for the concerned time period.

Answered by FredA in 776924022

You have to enable these notifications before you load the persistent store. For more details see [https://stackoverflow.com/questions/61790440/coredata-and-cloudkit-sync-works-but-nspersistentstoreremotechange-notification)

By mistake i set the store descriptions inside if (error ! = nil)

I changed that... yet no go...

- (NSPersistentCloudKitContainer *)persistentContainer {

    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.

    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentCloudKitContainer alloc] initWithName:@"Expenses"];            
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                   
#ifdef DEBUG
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
#endif
                    abort();
                }
                else
#ifdef DEBUG
                    NSLog(@"Store successfully initialized");
#endif
                [storeDescription setOption:[NSNumber numberWithBool:YES] forKey:@"PersistentHistoryTracking"];
                [storeDescription setOption:[NSNumber numberWithBool:YES] forKey:@"NSPersistentStoreRemoteChangeNotificationOptionKey"];
            }];
        }
    }    
    return _persistentContainer;
}

I think in built constants have to be used for keys so i changed the code to following...

- (NSPersistentCloudKitContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentCloudKitContainer alloc] initWithName:@"Expenses"];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error)
             {
                if (error != nil)
                {
                    
#ifdef DEBUG
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
#endif
                    abort();
                }
                else
                {
#ifdef DEBUG
                    NSLog(@"Store successfully initialized");
#endif
                    [storeDescription setOption:[NSNumber numberWithBool:YES] forKey:NSPersistentHistoryTrackingKey];
                    [storeDescription setOption:[NSNumber numberWithBool:YES] forKey:NSPersistentStoreRemoteChangeNotificationPostOptionKey];
                }
            }];
        }
        
        return _persistentContainer;
    }
}

No errors or warnings. just no notifications!

Can I observe any other cloudkit notification (like one fired when changes are synced in background)?

Accepted Answer

You have to enable these notifications before you load the persistent store. For more details see [https://stackoverflow.com/questions/61790440/coredata-and-cloudkit-sync-works-but-nspersistentstoreremotechange-notification)

Not receiving Persistent Store Remote Change Notification.
 
 
Q