Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Watching Core Data objects for updates or deletions
//
// CDZObjectWatcher.h
//
// Created by Chris Dzombak.
// Copyright (c) 2015 Chris Dzombak. All rights reserved.
//
@import Foundation;
@import CoreData;
typedef NS_ENUM(NSUInteger, CDZObjectWatchType) {
CDZObjectWatchTypeDeleted = 0,
CDZObjectWatchTypeUpdated,
};
typedef void(^CDZWatchedObjectsBlock)(NSSet *objects);
@interface CDZObjectWatcher : NSObject
/**
Initialize a watcher that watches the given Core Data store coordinator for
changes of the given type and executes the given block when a change occurs.
Your block will be called on the main queue.
*/
- (instancetype)initWithPersistentStoreCoordinator:(NSPersistentStoreCoordinator *)psc watchType:(CDZObjectWatchType)watchType block:(CDZWatchedObjectsBlock)block;
- (void)watchObject:(NSManagedObject *)object;
- (void)watchObjectsInArray:(NSArray *)objects;
- (void)watchObjectsInSet:(NSSet *)objects;
- (void)stopWatchingObject:(NSManagedObject *)object;
@end
//
// CDZObjectWatcher.m
//
// Created by Chris Dzombak.
// Copyright (c) 2015 Chris Dzombak. All rights reserved.
//
// This class's design and API are heavily based on MCDeleteWatcher,
// found at http://www.cimgf.com/2014/02/25/deleting-objects-in-core-data/
//
// Created by Marcus S. Zarra on 2/24/14.
// Copyright (c) 2014 MartianCraft. All rights reserved.
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#import "CDZObjectWatcher.h"
#import <dispatch/dispatch.h>
@interface CDZObjectWatcher ()
@property (nonatomic, readonly, weak) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, readonly) NSMutableSet *objectsToWatch;
@property (nonatomic, readonly) CDZWatchedObjectsBlock watchedObjectsBlock;
@property (nonatomic, readonly) CDZObjectWatchType watchType;
@end
@implementation CDZObjectWatcher
#pragma mark - Object Lifecycle
- (id)initWithPersistentStoreCoordinator:(NSPersistentStoreCoordinator *)psc watchType:(CDZObjectWatchType)watchType block:(CDZWatchedObjectsBlock)objectsBlock {
NSParameterAssert(psc);
NSParameterAssert(objectsBlock);
self = [super init];
if (self) {
_persistentStoreCoordinator = psc;
_watchedObjectsBlock = [objectsBlock copy];
_watchType = watchType;
_objectsToWatch = [NSMutableSet set];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextUpdated:) name:NSManagedObjectContextObjectsDidChangeNotification object:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
+ (dispatch_queue_t)backgroundQueue {
static dispatch_queue_t q = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
q = dispatch_queue_create("com.cdz.objectwatcher", DISPATCH_QUEUE_SERIAL);
});
return q;
}
#pragma mark - Public API
- (void)watchObject:(NSManagedObject *)object {
NSParameterAssert(object);
dispatch_async([[self class] backgroundQueue], ^{
[[self objectsToWatch] addObject:object];
});
}
- (void)watchObjectsInArray:(NSArray *)objects {
NSParameterAssert(objects);
dispatch_async([[self class] backgroundQueue], ^{
[[self objectsToWatch] addObjectsFromArray:objects];
});
}
- (void)watchObjectsInSet:(NSSet *)objects {
NSParameterAssert(objects);
dispatch_async([[self class] backgroundQueue], ^{
[[self objectsToWatch] unionSet:objects];
});
}
- (void)stopWatchingObject:(NSManagedObject *)object {
NSParameterAssert(object);
dispatch_async([[self class] backgroundQueue], ^{
[[self objectsToWatch] removeObject:object];
});
}
#pragma mark - Notification Handling
- (void)contextUpdated:(NSNotification *)notification {
if (!self.watchedObjectsBlock) {
return;
}
if ([[notification object] persistentStoreCoordinator] != self.persistentStoreCoordinator) {
return;
}
NSString *objectsKey = self.watchType == CDZObjectWatchTypeDeleted ? NSDeletedObjectsKey : NSUpdatedObjectsKey;
NSSet *notificationObjects = [[notification userInfo] objectForKey:objectsKey];
dispatch_async([[self class] backgroundQueue], ^{
NSMutableSet *objects = [NSMutableSet set];
for (NSManagedObject *object in notificationObjects) {
if ([self.objectsToWatch containsObject:object]) {
[objects addObject:object];
}
}
if ([objects count]) {
dispatch_async(dispatch_get_main_queue(), ^{
[self watchedObjectsBlock](objects);
});
}
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment