AppsDev
AppsDev

Reputation: 12509

Core Data & iCloud: exception when NSPersistentStoreCoordinatorStoresWillChangeNotification received

I'm following the Apple's iCloud Programming Guide for Core Data, the Using the SQLite Store with iCloud section, and you are told there to listen for the NSPersistentStoreCoordinatorStoresWillChangeNotification iCloud event this way:

[[NSNotificationCenter defaultCenter] addObserverForName:NSPersistentStoreCoordinatorStoresWillChangeNotification
                                                  object:self.managedObjectContext.persistentStoreCoordinator
                                                   queue:[NSOperationQueue mainQueue]
                                              usingBlock:^(NSNotification *note) {
                                                  [self.managedObjectContext performBlock:^{
                                                      // disable user interface with setEnabled: or an overlay

                                                      if ([self.managedObjectContext hasChanges]) {
                                                          NSError *saveError;
                                                          if (![self.managedObjectContext save:&saveError]) {
                                                              NSLog(@"Save error: %@", saveError);
                                                          }
                                                      }
                                                      else {
                                                          // drop any managed object references
                                                          [self.managedObjectContext reset];
                                                      }
                                                  }];
                                              }];

I've put such code within my AppDelegate's application:didFinishLaunchingWithOptions: method and, when the notification is received, I get this exception:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can only use -performBlock: on an NSManagedObjectContext that was created with a queue.'

According to the document, this should work... what could am I doing wrong?

Thanks

Upvotes: 0

Views: 279

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70976

The error:

Can only use -performBlock: on an NSManagedObjectContext that was created with a queue.

...is quite clear. You cannot call performBlock: on a managed object context unless that context was created using one of the queue-type concurrency options. Meaning that when you created the context, you must have used initWithConcurrencyType: and used either NSPrivateQueueConcurrencyType or NSMainQueueConcurrencyType as the argument.

This has nothing at all to do with the notification, only with the fact that you're calling performBlock: when you didn't create the context correctly.

Upvotes: 2

Related Questions