-
Notifications
You must be signed in to change notification settings - Fork 0
/
TXMainThreadMOC.m
245 lines (192 loc) · 8.68 KB
/
TXMainThreadMOC.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
Copyright 2013 Tonny Xu
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "TXMainThreadMOC.h"
#import "TXMainThreadMOC_Definition.h"
@interface TXMainThreadMOC()
@property (nonatomic, retain, readwrite) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readwrite) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readwrite) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory;
- (void)prepopulateData;
@end
@implementation TXMainThreadMOC
+ (TXMainThreadMOC *)sharedInstance{
NSAssert([NSThread isMainThread], @"%s should be run on the main thread", __FUNCTION__);
static TXMainThreadMOC *MOCInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
MOCInstance = [[TXMainThreadMOC alloc] init];
});
return MOCInstance;
}
- (id)init{
NSAssert([NSThread isMainThread], @"%s should be run on the main thread", __FUNCTION__);
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:nil]; // it's very important to set sender object to nil to accept notification from any MOC object
}
return self;
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
//////////////////////////////////////////////////////////////////////////////
// CoreData properties.
// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext {
NSAssert([NSThread isMainThread], @"%s should be run on the main thread", __FUNCTION__);
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
/* NOTE By Tonny
* -------------
* Aug 9, 2012
*
* iOS 5 added new concurrency support for Core Data, instead of using
* [NSManageObjectContext new], use the -initWithConcurrencyType: method will
* make it more easy to understand.
*
*/
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator: coordinator];
_managedObjectContext.undoManager = nil;
_managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
[self prepopulateData];
}
return _managedObjectContext;
}
//////////////////////////////////////////////////////////////////////////////
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
- (NSManagedObjectModel *)managedObjectModel {
NSAssert([NSThread isMainThread], @"%s should be run on the main thread", __FUNCTION__);
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
self.managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return _managedObjectModel;
}
//////////////////////////////////////////////////////////////////////////////
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
NSAssert([NSThread isMainThread], @"%s should be run on the main thread", __FUNCTION__);
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSString *storePath = [[[self applicationDocumentsDirectory] path] stringByAppendingPathComponent:DBFileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn't exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:DBFileName ofType:nil];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
NSError *error;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
NSDictionary *migarationOptions = @{NSMigratePersistentStoresAutomaticallyOption:@(YES), NSInferMappingModelAutomaticallyOption:@(YES)};
// This method will create the DB if it is not existed.
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:migarationOptions error:&error]) {
// Typical reasons for an error here include:
// The persistent store is not accessible
// The schema for the persistent store is incompatible with current managed object model
// Check the error message to determine what the actual problem was.
//
abort();
}
return _persistentStoreCoordinator;
}
//////////////////////////////////////////////////////////////////////////////
// this is called via observing "NSManagedObjectContextDidSaveNotification" from SubThreadMOC
- (void)mergeChanges:(NSNotification *)notification {
if ([notification object] == self.managedObjectContext) {
// main context save, no need to perform the merge
return;
}
[self.managedObjectContext performBlock:^{
[self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
}];
}
- (void)saveContext
{
NSAssert([NSThread isMainThread], @"%s should be run on the main thread", __FUNCTION__);
NSError *error = nil;
NSManagedObjectContext *mainMoc = self.managedObjectContext;
if (mainMoc != nil)
{
if ([mainMoc hasChanges] && ![mainMoc save:&error])
{
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
abort();
}
}
}
- (void)prepopulateData{
// Add your code here to prepopulate data.
}
#ifdef DEBUG
- (void)deleteStoreFileAndRecreateStore{
NSAssert([NSThread isMainThread], @"%s should be run on the main thread", __FUNCTION__);
NSPersistentStore *store = [[self.persistentStoreCoordinator persistentStores] lastObject];
NSError *error;
NSURL *storeURL = store.URL;
NSPersistentStoreCoordinator *storeCoordinator = self.persistentStoreCoordinator;
[storeCoordinator removePersistentStore:store error:&error];
[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
_persistentStoreCoordinator = nil;
_managedObjectContext = nil;
}
#else
- (void)deleteStoreFileAndRecreateStore{}
#endif
#pragma mark - Application's Documents directory
/**
Returns the URL to the application's Documents directory.
*/
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
#pragma mark - SubMoc/ChildMoc helper
- (NSManagedObjectContext *)spawnSubMoc{
NSAssert(![NSThread isMainThread], @"%s should be run on a sub thread", __FUNCTION__);
NSManagedObjectContext *subMoc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
subMoc.persistentStoreCoordinator = self.persistentStoreCoordinator;
subMoc.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
return subMoc;
}
- (NSManagedObjectContext *)spawnMainThreadChildMoc{
NSAssert([NSThread isMainThread], @"%s should be run on the main thread", __FUNCTION__);
NSManagedObjectContext *childMoc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[childMoc setParentContext:self.managedObjectContext];
return childMoc;
}
- (NSManagedObjectContext *)spawnSubThreadChildMoc{
NSAssert(![NSThread isMainThread], @"%s should be run on a sub thread", __FUNCTION__);
NSManagedObjectContext *childMoc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[childMoc setParentContext:self.managedObjectContext];
return childMoc;
}
@end