Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 11th, 2012  |  syntax: None  |  size: 1.52 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How does one have a Singleton Observe Changes to Core Data Model?
  2. @implementation Singleton
  3.  
  4. + (Singleton *) sharedInstance
  5. {
  6.     static Singleton *sharedInstance = nil;
  7.     static dispatch_once_t onceToken;
  8.     dispatch_once(&onceToken, ^{
  9.         sharedInstance = [[Singleton alloc] init];
  10.  
  11.         // Do any other initialisation stuff here
  12.         AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
  13.         NSManagedObjectContext *myManagedObjectContext = appDelegate.managedObjectContext;
  14.  
  15.         [[NSNotificationCenter defaultCenter] addObserver:self
  16.                                              selector:@selector(handleDataModelChange:)
  17.                                     name:NSManagedObjectContextObjectsDidChangeNotification
  18.                                                    object:myManagedObjectContext];
  19.  
  20.     });
  21.     return sharedInstance;
  22. }
  23.  
  24. - (void)handleDataModelChange:(NSNotification *)note;
  25. {
  26.     NSSet *updatedObjects = [[note userInfo] objectForKey:NSUpdatedObjectsKey];
  27.     NSSet *deletedObjects = [[note userInfo] objectForKey:NSDeletedObjectsKey];
  28.     NSSet *insertedObjects = [[note userInfo] objectForKey:NSInsertedObjectsKey];
  29.  
  30.     // Do something in response to this
  31.     NSLog(@"%@ Objects Updated, %@ Objects Deleted, %@ Objects Inserted",[updatedObjects count],[deletedObjects count],[insertedObjects count]);
  32. }
  33.  
  34. @end
  35.        
  36. - (void)handleDataModelChange:(NSNotification *)note;
  37.        
  38. Deleted, %@ Objects Inserted",[updatedObjects count],[deletedObjects count],[insertedObjects count]);