-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathActiveManager.m
457 lines (319 loc) · 14.2 KB
/
ActiveManager.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//
// ActiveManager.m
// Shopify_Mobile
//
// Created by Matthew Newberry on 8/2/10.
// Copyright 2010 Shopify. All rights reserved.
//
#import "ActiveManager.h"
#import "ActiveConnection.h"
#import "NSThread+Blocks.m"
#define OR_CORE_DATE_STORE_TYPE NSSQLiteStoreType
#define OR_CORE_DATE_STORE_NAME @"CoreDataStore.sqlite"
#define OR_CORE_DATE_BATCH_SIZE 25
#define OR_MOC_KEY @"managedObjectContext"
#define OR_CORE_DATA_MIGRATION_NEED @"coreDataMigrationNeeded"
#define OR_SEED_DIR @"Seeds"
static ActiveManager *_shared = nil;
@implementation ActiveManager
@synthesize remoteContentFormat = _remoteContentFormat;
@synthesize remoteContentType = _remoteContentType;
@synthesize parsingClass = _parsingClass;
@synthesize baseRemoteURL = _baseRemoteURL;
@synthesize connectionClass = _connectionClass;
@synthesize logLevel = _logLevel;
@synthesize defaultDateParser = _defaultDateParser;
@synthesize defaultNumberFormatter = _defaultNumberFormatter;
@synthesize entityDescriptions = _entityDescriptions;
@synthesize modelProperties = _modelProperties;
@synthesize modelRelationships = _modelRelationships;
@synthesize modelAttributes = _modelAttributes;
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
@synthesize fixtureParsingClass = _fixtureParsingClass;
+ (ActiveManager *) shared{
if(_shared == nil)
_shared = [[ActiveManager alloc] init];
return _shared;
}
- (id) init{
if((self = [super init])){
self.remoteContentType = @"application/json";
self.remoteContentFormat = @"json";
self.managedObjectModel = [self managedObjectModel];
self.persistentStoreCoordinator = [self persistentStoreCoordinator];
_managedObjectContext = [self newManagedObjectContext];
_defaultDateParser = [[NSDateFormatter alloc] init];
[_defaultDateParser setTimeZone:[NSTimeZone localTimeZone]];
_defaultNumberFormatter = [[NSNumberFormatter alloc] init];
self.entityDescriptions = [NSMutableDictionary dictionary];
self.modelProperties = [NSMutableDictionary dictionary];
self.modelRelationships = [NSMutableDictionary dictionary];
self.logLevel = 2;
}
return self;
}
- (void) setConnectionClass:(id)activeConnectionClass{
_connectionClass = activeConnectionClass;
}
- (void) addRequest:(ActiveRequest *) request{
[self addRequest:request delegate:request.delegate didParseObjectSelector:request.didParseObjectSelector didFinishSelector:request.didFinishSelector didFailSelector:request.didFailSelector];
}
- (void) addBaseURL:(ActiveRequest *)request{
[request setUrlPath:[[ActiveManager shared].baseRemoteURL stringByAppendingString:request.urlPath]];
}
- (void) addRequest:(ActiveRequest *) request delegate:(id) delegate didParseObjectSelector:(SEL)didParseObjectSelector didFinishSelector:(SEL)didFinishSelector didFailSelector:(SEL)didFailSelector{
if([request.urlPath rangeOfString:@"http"].length == 0)
[self addBaseURL:request];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
id <ActiveConnection> conn = [[[_connectionClass alloc] init] autorelease];
[conn setResponseDelegate:delegate];
[conn setDidFinishSelector:didFinishSelector];
[conn setDidFailSelector:didFailSelector];
[conn send:request];
});
}
- (void) addRequest:(ActiveRequest *) request didParseObjectBlock:(ActiveConnectionDidParseObjectBlock)didParseObjectBlock didFinishBlock:(ActiveConnectionBlock)didFinishBlock didFailBlock:(ActiveConnectionBlock)didFailBlock{
[self addRequest:request toQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) didParseObjectBlock:didParseObjectBlock didFinishBlock:didFinishBlock didFailBlock:didFailBlock];
}
- (void) addRequest:(ActiveRequest *) request toQueue:(dispatch_queue_t)queue didParseObjectBlock:(ActiveConnectionDidParseObjectBlock)didParseObjectBlock didFinishBlock:(ActiveConnectionBlock)didFinishBlock didFailBlock:(ActiveConnectionBlock)didFailBlock{
if([request.urlPath rangeOfString:@"http"].length == 0)
[self addBaseURL:request];
if(dispatch_get_current_queue() == queue){
NSObject <ActiveConnection> *conn = [[_connectionClass alloc] init];
[conn setResponseDelegate:request.delegate];
[conn setDidFailBlock:didFailBlock];
[conn setDidFinishBlock:didFinishBlock];
[conn setDidParseObjectBlock:didParseObjectBlock];
[conn send:request];
[conn release];
}
else
dispatch_async(queue, ^{
id <ActiveConnection> conn = [[[_connectionClass alloc] init] autorelease];
[conn setResponseDelegate:request.delegate];
[conn setDidFailBlock:didFailBlock];
[conn setDidFinishBlock:didFinishBlock];
[conn setDidParseObjectBlock:didParseObjectBlock];
[conn send:request];
});
}
- (ActiveResult *) addSyncronousRequest:(ActiveRequest *)request{
if([request.urlPath rangeOfString:@"http"].length == 0)
[self addBaseURL:request];
return [[[_connectionClass new] autorelease] performSelector:@selector(sendSyncronously:) withObject:request];
}
- (void) addSyncronousRequest:(ActiveRequest *)request delegate:(id) delegate didFinishSelector:(SEL)didFinishSelector didFailSelector:(SEL)didFailSelector{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if([request.urlPath rangeOfString:@"http"].length == 0)
[self addBaseURL:request];
ActiveResult *result = [[[_connectionClass new] autorelease] performSelector:@selector(sendSyncronously:) withObject:request];
if(result.error != nil)
[delegate performSelector:didFinishSelector withObject:result];
else
[delegate performSelector:didFailSelector withObject:result];
[pool release];
}
- (void) addSyncronousRequest:(ActiveRequest *)request didFinishBlock:(ActiveConnectionBlock)didFinishBlock didFailBlock:(ActiveConnectionBlock)didFailBlock{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if([request.urlPath rangeOfString:@"http"].length == 0)
[self addBaseURL:request];
ActiveResult *result = [[[_connectionClass new] autorelease] performSelector:@selector(sendSyncronously:) withObject:request];
if(result.error != nil)
[NSThread performBlockOnMainThread:^{
didFinishBlock(result);
}];
else
[NSThread performBlockOnMainThread:^{
didFailBlock(result);
}];
[pool release];
}
- (NSData *) serializeObject:(id)object{
id <ActiveParser> parser = [[[_parsingClass alloc] init] autorelease];
NSString *string = [parser parseToString:object];
return [string dataUsingEncoding:NSUTF8StringEncoding];
}
- (id) parseString:(NSString *) content{
id <ActiveParser> parser = [[[_parsingClass alloc] init] autorelease];
return [parser parse:content];
}
- (id) parseFixture:(NSString *) content{
if(_fixtureParsingClass != nil){
id <ActiveParser> parser = [[[_fixtureParsingClass alloc] init] autorelease];
return [parser parse:content];
}
else
return [self parseString:content];
}
/* Core Data */
- (void) managedObjectContextDidSave:(NSNotification *)notification{
[self.managedObjectContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
withObject:notification
waitUntilDone:YES];
}
- (void)mergeChanges:(NSNotification *)notification {
[self performSelectorOnMainThread:@selector(managedObjectContextDidSave:) withObject:notification waitUntilDone:YES];
}
- (NSManagedObjectContext*) newManagedObjectContext{
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
[moc setPersistentStoreCoordinator:self.persistentStoreCoordinator];
[moc setUndoManager:nil];
[moc setMergePolicy:NSOverwriteMergePolicy];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:moc];
return moc;
}
- (NSManagedObjectContext*) managedObjectContext {
if ([NSThread isMainThread]) {
return _managedObjectContext;
} else {
NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];
NSManagedObjectContext *backgroundThreadContext = [threadDictionary objectForKey:OR_MOC_KEY];
if (!backgroundThreadContext) {
backgroundThreadContext = [self newManagedObjectContext];
[threadDictionary setObject:backgroundThreadContext forKey:OR_MOC_KEY];
[backgroundThreadContext release];
}
return backgroundThreadContext;
}
}
- (NSManagedObjectModel*) managedObjectModel {
if( _managedObjectModel != nil )
return _managedObjectModel;
NSURL *momURL = [NSURL fileURLWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"Shopify_Mobile" ofType:@"momd"]];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
if(_managedObjectModel == nil)
_managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
return _managedObjectModel;
}
- (NSString*) storePath {
return [[self applicationDocumentsDirectory]
stringByAppendingPathComponent: OR_CORE_DATE_STORE_NAME];
}
- (NSURL*) storeUrl {
return [NSURL fileURLWithPath:[self storePath]];
}
- (NSDictionary*) migrationOptions {
return $D([NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption);
}
- (NSPersistentStoreCoordinator*) persistentStoreCoordinator {
if( _persistentStoreCoordinator != nil ) {
return _persistentStoreCoordinator;
}
NSString* storePath = [self storePath];
NSURL *storeUrl = [self storeUrl];
NSError* error;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel: _managedObjectModel];
NSDictionary* options = [self migrationOptions];
// Check whether the store already exists or not.
NSFileManager* fileManager = [NSFileManager defaultManager];
BOOL exists = [fileManager fileExistsAtPath:storePath];
if(!exists ) {
_modelCreated = YES;
} else {
if( _resetModel ||
[[NSUserDefaults standardUserDefaults] boolForKey:@"erase_all_preference"] ) {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"erase_all_preference"];
[fileManager removeItemAtPath:storePath error:nil];
_modelCreated = YES;
}
}
BOOL compatible = [_managedObjectModel isConfiguration:nil compatibleWithStoreMetadata:[NSPersistentStoreCoordinator metadataForPersistentStoreOfType:OR_CORE_DATE_STORE_TYPE URL:storeUrl error:&error]];
if(!compatible)
[[NSNotificationCenter defaultCenter] postNotificationName:OR_CORE_DATA_MIGRATION_NEED object:nil];
if (![_persistentStoreCoordinator
addPersistentStoreWithType: OR_CORE_DATE_STORE_TYPE
configuration: nil
URL: storeUrl
options: options
error: &error
]) {
// We couldn't add the persistent store, so let's wipe it out and try again.
[fileManager removeItemAtPath:storePath error:nil];
_modelCreated = YES;
if (![_persistentStoreCoordinator
addPersistentStoreWithType: OR_CORE_DATE_STORE_TYPE
configuration: nil
URL: storeUrl
options: options
error: &error
]) {
// Something is terribly wrong here.
}
}
return _persistentStoreCoordinator;
}
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
+ (NSArray *) seedFiles{
return [[NSFileManager defaultManager] contentsOfDirectoryAtPath:$S(@"%@/%@", [[NSBundle bundleForClass:[self class]] bundlePath], OR_SEED_DIR) error:nil];
}
- (BOOL) loadAllSeedFiles{
return [self loadSeedFilesForGroupName:nil];
}
- (BOOL) loadSeedFilesForGroupName:(NSString *) groupName{
return [self loadSeedFiles:[[self class] seedFiles] groupName:groupName];
}
- (BOOL) loadSeedFiles:(NSArray *) files groupName:(NSString *) groupName{
NSError *error = nil;
for(NSString *file in files){
NSString *content = [NSString stringWithContentsOfFile:$S(@"%@/%@/%@", [[NSBundle bundleForClass:[self class]] bundlePath], OR_SEED_DIR, file) encoding:NSUTF8StringEncoding error:&error];
id value = [self parseFixture:content];
if(value == nil){
error = [NSError errorWithDomain:@"com.objectiverecord" code:1 userInfo:$D(file, content)];
continue;
}
NSMutableString *class = [NSMutableString stringWithString:[[[file stringByDeletingPathExtension] componentsSeparatedByString:@"_"] objectAtIndex:0]];
[class replaceOccurrencesOfString:[ActiveRecord classPrefix] withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [[ActiveRecord classPrefix] length])];
Class modelClass = NSClassFromString(class);
[modelClass removeAll];
if([value isKindOfClass:[NSDictionary class]]){
[value enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
if(groupName == nil || (groupName != nil && [groupName isEqualToString:key]))
[modelClass build:obj];
}];
}
else if(groupName == nil)
[modelClass build:value];
}
[ActiveRecord save];
return error == nil;
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; // denotes an object that cannot be released
}
- (void)release {
//do nothing
}
- (id)autorelease {
return self;
}
- (void)dealloc{
[_managedObjectContext release];
[_managedObjectModel release];
[_persistentStoreCoordinator release];
[_defaultDateParser release];
[_defaultNumberFormatter release];
[_entityDescriptions release];
[_modelProperties release];
[_modelRelationships release];
[_modelAttributes release];
[_baseRemoteURL release];
[_parsingClass release];
[_fixtureParsingClass release];
[_remoteContentType release];
[_remoteContentFormat release];
[super dealloc];
}
@end