Core Data with iCloud Notifications in iOS 7

Viewed 1487

I have added iCloud support to my app. when the user update his data I got a notification NSPersistentStoreDidImportUbiquitousContentChangesNotification

That's great

However, I have these problems

  1. I can't find a will version of this notification that I can use to show loader for example that the database will be updated
  2. Since I can't find a will version, then I used the same notification to display a loader to tell the user that the database is being updated ... The problem in this notification is that it can be called several time if the content changed in iCloud is relatively large which lead me to display several loading indicator !
  3. Is there a way that I can use to trigger the update through iCloud manually?

.


I added iCloud support to Core data by doing this:

in NSManagedObjectContext

_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
_managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;

in NSPersistentStoreCoordinator

[_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{NSPersistentStoreUbiquitousContentNameKey:@"iCloudStore"} error:nil];

and In NSPersistentStoreDidImportUbiquitousContentChangesNotification notification method

- (void)persistentStoreDidImportUbiquitousContentChanges:(NSNotification *)notification
{
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    [managedObjectContext performBlock:^{
        [managedObjectContext mergeChangesFromContextDidSaveNotification:notification];

        dispatch_async(dispatch_get_main_queue(), ^{
            UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
            MBProgressHUD *hud = [MBProgressHUD HUDForView:mainWindow];
            if (hud == nil) {
                hud = [[MBProgressHUD alloc] initWithWindow:mainWindow];
            }
            [mainWindow addSubview:hud];
            hud.labelText = NSLocalizedString(@"Progress updated", nil);
            [hud show:YES];
            [hud hide:YES afterDelay:1];

            // Post Notification
            [[NSNotificationCenter defaultCenter] postNotificationName:RESET_APPLICATION_NOTIFICATION object:nil];
        });
    }];
}
1 Answers
Related