
Untitled
By: a guest on
Aug 11th, 2012 | syntax:
None | size: 1.52 KB | hits: 8 | expires: Never
How does one have a Singleton Observe Changes to Core Data Model?
@implementation Singleton
+ (Singleton *) sharedInstance
{
static Singleton *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[Singleton alloc] init];
// Do any other initialisation stuff here
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *myManagedObjectContext = appDelegate.managedObjectContext;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleDataModelChange:)
name:NSManagedObjectContextObjectsDidChangeNotification
object:myManagedObjectContext];
});
return sharedInstance;
}
- (void)handleDataModelChange:(NSNotification *)note;
{
NSSet *updatedObjects = [[note userInfo] objectForKey:NSUpdatedObjectsKey];
NSSet *deletedObjects = [[note userInfo] objectForKey:NSDeletedObjectsKey];
NSSet *insertedObjects = [[note userInfo] objectForKey:NSInsertedObjectsKey];
// Do something in response to this
NSLog(@"%@ Objects Updated, %@ Objects Deleted, %@ Objects Inserted",[updatedObjects count],[deletedObjects count],[insertedObjects count]);
}
@end
- (void)handleDataModelChange:(NSNotification *)note;
Deleted, %@ Objects Inserted",[updatedObjects count],[deletedObjects count],[insertedObjects count]);