Before the code...
- CloudKit, background mode (remote notifications) and push notifications are added to Capabilities.
- 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.
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)