forked from magicalpanda/MagicalRecord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSManagedObjectContext+ActiveRecord.m
209 lines (181 loc) · 5.96 KB
/
NSManagedObjectContext+ActiveRecord.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// NSManagedObjectContext+ActiveRecord.m
//
// Created by Saul Mora on 11/23/09.
// Copyright 2010 Magical Panda Software, LLC All rights reserved.
//
#import "NSManagedObject+ActiveRecord.h"
#import "NSManagedObjectContext+ActiveRecord.h"
#import "NSPersistentStoreCoordinator+ActiveRecord.h"
#import <objc/runtime.h>
static NSManagedObjectContext *defaultManageObjectContext = nil;
static NSString const * kActiveRecordManagedObjectContextKey = @"ActiveRecord_NSManagedObjectContextForThreadKey";
@implementation NSManagedObjectContext (ActiveRecord)
+ (NSManagedObjectContext *)defaultContext
{
// NSAssert([NSThread isMainThread], @"The defaultContext must only be accessed on the **Main Thread**");
@synchronized (self)
{
if (defaultManageObjectContext)
{
return defaultManageObjectContext;
}
}
return nil;
}
+ (void) setDefaultContext:(NSManagedObjectContext *)moc
{
if (defaultManageObjectContext != moc) {
[defaultManageObjectContext release];
defaultManageObjectContext = [moc retain];
}
}
+ (void) resetDefaultContext
{
dispatch_sync(dispatch_get_main_queue(), ^{
[[NSManagedObjectContext defaultContext] reset];
});
}
+ (void) resetContextForCurrentThread {
[[NSManagedObjectContext contextForCurrentThread] reset];
}
+ (NSManagedObjectContext *) contextForCurrentThread
{
if ( [NSThread isMainThread] )
{
return [self defaultContext];
}
else
{
NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
NSManagedObjectContext *threadContext = [threadDict objectForKey:kActiveRecordManagedObjectContextKey];
if ( threadContext == nil )
{
threadContext = [self contextThatNotifiesDefaultContextOnMainThread];
[threadDict setObject:threadContext forKey:kActiveRecordManagedObjectContextKey];
}
return threadContext;
}
}
- (void) observeContext:(NSManagedObjectContext *)otherContext
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mergeChangesFromNotification:)
name:NSManagedObjectContextDidSaveNotification
object:otherContext];
}
- (void) observeContextOnMainThread:(NSManagedObjectContext *)otherContext
{
// ARLog(@"Start Observing on Main Thread");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mergeChangesOnMainThread:)
name:NSManagedObjectContextDidSaveNotification
object:otherContext];
}
- (void) stopObservingContext:(NSManagedObjectContext *)otherContext
{
// ARLog(@"Stop Observing Context");
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSManagedObjectContextDidSaveNotification
object:otherContext];
}
- (void) mergeChangesFromNotification:(NSNotification *)notification
{
ARLog(@"Merging changes to %@context%@",
self == [NSManagedObjectContext defaultContext] ? @"*** DEFAULT *** " : @"",
([NSThread isMainThread] ? @" *** on Main Thread ***" : @""));
[self mergeChangesFromContextDidSaveNotification:notification];
}
- (void) mergeChangesOnMainThread:(NSNotification *)notification
{
if ([NSThread isMainThread])
{
[self mergeChangesFromNotification:notification];
}
else
{
[self performSelectorOnMainThread:@selector(mergeChangesFromNotification:) withObject:notification waitUntilDone:YES];
}
}
- (BOOL) save
{
NSError *error = nil;
BOOL saved = NO;
@try
{
ARLog(@"Saving %@Context%@",
self == [[self class] defaultContext] ? @" *** Default *** ": @"",
([NSThread isMainThread] ? @" *** on Main Thread ***" : @""));
saved = [self save:&error];
}
@catch (NSException *exception)
{
ARLog(@"Problem saving: %@", (id)[exception userInfo] ?: (id)[exception reason]);
}
[ActiveRecordHelpers handleErrors:error];
return saved && error == nil;
}
- (void) saveWrapper
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self save];
[pool drain];
}
- (BOOL) saveOnBackgroundThread
{
[self performSelectorInBackground:@selector(saveWrapper) withObject:nil];
return YES;
}
- (BOOL) saveOnMainThread
{
@synchronized(self)
{
[self performSelectorOnMainThread:@selector(saveWrapper) withObject:nil waitUntilDone:YES];
}
return YES;
}
- (BOOL) notifiesMainContextOnSave
{
NSNumber *notifies = objc_getAssociatedObject(self, @"notifiesMainContext");
return notifies ? [notifies boolValue] : NO;
}
- (void) setNotifiesMainContextOnSave:(BOOL)enabled
{
NSManagedObjectContext *mainContext = [[self class] defaultContext];
if (self != mainContext)
{
SEL selector = enabled ? @selector(observeContextOnMainThread:) : @selector(stopObservingContext:);
objc_setAssociatedObject(self, @"notifiesMainContext", [NSNumber numberWithBool:enabled], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[mainContext performSelector:selector withObject:self];
}
}
+ (NSManagedObjectContext *) contextWithStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator
{
NSManagedObjectContext *context = nil;
if (coordinator != nil)
{
ARLog(@"Creating MOContext %@", [NSThread isMainThread] ? @" *** On Main Thread ***" : @"");
context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:coordinator];
}
return [context autorelease];
}
+ (NSManagedObjectContext *) contextThatNotifiesDefaultContextOnMainThreadWithCoordinator:(NSPersistentStoreCoordinator *)coordinator;
{
NSManagedObjectContext *context = [self contextWithStoreCoordinator:coordinator];
// [[self defaultContext] observeContext:context];
context.notifiesMainContextOnSave = YES;
return context;
}
+ (NSManagedObjectContext *) context
{
return [self contextWithStoreCoordinator:[NSPersistentStoreCoordinator defaultStoreCoordinator]];
}
+ (NSManagedObjectContext *) contextThatNotifiesDefaultContextOnMainThread
{
NSManagedObjectContext *context = [self context];
// [[self defaultContext] observeContextOnMainThread:context];
context.notifiesMainContextOnSave = YES;
return context;
}
@end