Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 123 lines (106 sloc) 4.648 kb
572b86a Initial commit of common code
Marcus S. Zarra authored
1 //
2 // ZSContextWatcher.m
3 //
4 // Copyright 2010 Zarra Studios, LLC All rights reserved.
5 //
6 // Permission is hereby granted, free of charge, to any person
7 // obtaining a copy of this software and associated documentation
8 // files (the "Software"), to deal in the Software without
9 // restriction, including without limitation the rights to use,
10 // copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the
12 // Software is furnished to do so, subject to the following
13 // conditions:
14 //
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 // OTHER DEALINGS IN THE SOFTWARE.
26
27 #import "ZSContextWatcher.h"
28
29 @implementation ZSContextWatcher
30
31 - (id)initWithManagedObjectContext:(NSManagedObjectContext*)context;
32 {
33 ZAssert(context, @"Context is nil!");
34 [super init];
35
36 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextUpdated:) name:NSManagedObjectContextDidSaveNotification object:nil];
37
38 persistentStoreCoordinator = [context persistentStoreCoordinator];
39
40 return self;
41 }
42
43 - (void) dealloc
44 {
45 [[NSNotificationCenter defaultCenter] removeObserver:self];
46 delegate = nil;
47 [masterPredicate release], masterPredicate = nil;
48 [super dealloc];
49 }
50
51 - (void)addEntityToWatch:(NSEntityDescription*)description withPredicate:(NSPredicate*)predicate;
52 {
53 NSPredicate *entityPredicate = [NSPredicate predicateWithFormat:@"entity.name == %@", [description name]];
54 NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:entityPredicate, predicate, nil]];
55
56 if (![self masterPredicate]) {
57 [self setMasterPredicate:finalPredicate];
58 return;
59 }
60
61 NSArray *array = [[NSArray alloc] initWithObjects:[self masterPredicate], finalPredicate, nil];
62 finalPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:array];
63 [array release], array = nil;
64 [self setMasterPredicate:finalPredicate];
65 }
66
67 - (void)contextUpdated:(NSNotification*)notification
68 {
69 NSManagedObjectContext *incomingContext = [notification object];
a867e42 Tweak to context watcher delegate notifications
Marcus S. Zarra authored
70 NSPersistentStoreCoordinator *incomingCoordinator = [incomingContext persistentStoreCoordinator];
71 if (incomingCoordinator != [self persistentStoreCoordinator]) {
72 return;
73 }
572b86a Initial commit of common code
Marcus S. Zarra authored
74 if ([self reference]) {
75 DLog(@"%@ entered", [self reference]);
76 }
77 NSMutableSet *inserted = [[[notification userInfo] objectForKey:NSInsertedObjectsKey] mutableCopy];
78 [inserted filterUsingPredicate:[self masterPredicate]];
79 NSMutableSet *deleted = [[[notification userInfo] objectForKey:NSDeletedObjectsKey] mutableCopy];
80 [deleted filterUsingPredicate:[self masterPredicate]];
81 NSMutableSet *updated = [[[notification userInfo] objectForKey:NSUpdatedObjectsKey] mutableCopy];
82 [updated filterUsingPredicate:[self masterPredicate]];
83
84 NSInteger totalCount = [inserted count] + [deleted count] + [updated count];
85 if (totalCount == 0) {
86 [inserted release], inserted = nil;
87 [deleted release], deleted = nil;
88 [updated release], updated = nil;
89 if ([self reference]) {
90 DLog(@"%@----------fail on count", [self reference]);
91 }
92 return;
93 }
94
95 NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
96 if (inserted) [results setObject:inserted forKey:NSInsertedObjectsKey];
97 if (deleted) [results setObject:deleted forKey:NSDeletedObjectsKey];
98 if (updated) [results setObject:updated forKey:NSUpdatedObjectsKey];
99
100 if ([[self delegate] respondsToSelector:[self action]]) {
101 if ([self reference]) {
102 DLog(@"%@++++++++++firing action", [self reference]);
103 }
a867e42 Tweak to context watcher delegate notifications
Marcus S. Zarra authored
104 [[self delegate] performSelectorOnMainThread:[self action] withObject:self waitUntilDone:YES];
572b86a Initial commit of common code
Marcus S. Zarra authored
105 } else {
106 if ([self reference]) {
107 DLog(@"%@----------delegate doesn't respond", [self reference]);
108 }
109 }
110 [results release], results = nil;
111 [inserted release], inserted = nil;
112 [deleted release], deleted = nil;
113 [updated release], updated = nil;
114 }
115
116 @synthesize persistentStoreCoordinator;
117 @synthesize delegate;
118 @synthesize action;
119 @synthesize masterPredicate;
120 @synthesize reference;
121
122 @end
Something went wrong with that request. Please try again.