Reputation: 14158
I'm new to Core Data and iCloud, but I've managed to get both working (mostly). My data is syncing between iOS devices, but I don't know when. :)
I'm trying to listen for when changes are available from iCloud so I can refresh my UI.
I set up Core Data and iCloud in my app delegate like this: http://d.pr/n/SxmZ Most of that is sample code I have applied, but it seems to work well.
In a separate view controller, where I'm displaying app data, I have set up my notification like this:
- (void)viewDidLoad
{
[super viewDidLoad];
//Register notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatesAvailable) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:nil];
}
- (void)updatesAvailable{
//This never fires...
NSLog(@"Update available...");
[PPStatusBar showSuccessWithStatus:@"Update available..."];
}
So here are my questions:
updatesAvailable
method, so has the data from iCloud made it all the way into the Core Data object graph by the time NSPersistentStoreDidImportUbiquitousContentChangesNotification
is called?I have tried this between two iOS 7 devices, in addition to the iOS 7 Simulator, and still no luck. I have also managed to get iCloud key/value change listeners work great, it's just not working with Core Data.
Thanks in advance for your help. :)
Upvotes: 0
Views: 350
Reputation: 1095
It looks like the problem is in your addObserver call. The object is nil but it should be your persistent store coordinator. For example:
[notificationCentre addObserver:self
selector:@selector(CoreData_StoreDidImportUbiquitousContentChanges:)
name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
object:coordinator];
Which then calls a notification handler structured like this:
- (void)CoreData_StoreDidImportUbiquitousContentChanges:(NSNotification*) notification {
}
To answer your specific questions:
One further note. The import notification will come in on a different thread to your user interface. If you want to make any UI changes (eg. refreshing) then you will need to use something like this to make sure they run on the UI thread:
dispatch_async(dispatch_get_main_queue(), ^{
// Refresh UI here
});
Upvotes: 1