-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathActiveRecordHelpers.m
149 lines (126 loc) · 4.39 KB
/
ActiveRecordHelpers.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
//
// ActiveRecordHelpers.m
//
// Created by Saul Mora on 3/11/10.
// Copyright 2010 Magical Panda Software, LLC All rights reserved.
//
#import "ActiveRecordHelpers.h"
#import "ARCoreDataAction.h"
#import "NSManagedObjectContext+ActiveRecord.h"
#import "NSPersistentStoreCoordinator+ActiveRecord.h"
#import "NSManagedObjectModel+ActiveRecord.h"
#import "NSPersistentStore+ActiveRecord.h"
#import <dispatch/dispatch.h>
static id errorHandlerTarget = nil;
static SEL errorHandlerAction = nil;
@implementation ActiveRecordHelpers
+ (void) cleanUp
{
[ARCoreDataAction cleanUp];
[NSManagedObjectContext setDefaultContext:nil];
[NSManagedObjectModel setDefaultManagedObjectModel:nil];
[NSPersistentStoreCoordinator setDefaultStoreCoordinator:nil];
[NSPersistentStore setDefaultPersistentStore:nil];
}
+ (void) defaultErrorHandler:(NSError *)error
{
NSDictionary *userInfo = [error userInfo];
for (NSArray *detailedError in [userInfo allValues])
{
if ([detailedError isKindOfClass:[NSArray class]])
{
for (NSError *e in detailedError)
{
if ([e respondsToSelector:@selector(userInfo)])
{
ARLog(@"Error Details: %@", [e userInfo]);
}
else
{
ARLog(@"Error Details: %@", e);
}
}
}
else
{
ARLog(@"Error: %@", detailedError);
}
}
ARLog(@"Error Domain: %@", [error domain]);
ARLog(@"Recovery Suggestion: %@", [error localizedRecoverySuggestion]);
}
+ (void) handleErrors:(NSError *)error
{
if (error)
{
// If a custom error handler is set, call that
if (errorHandlerTarget != nil && errorHandlerAction != nil)
{
[errorHandlerTarget performSelector:errorHandlerAction withObject:error];
}
else
{
// Otherwise, fall back to the default error handling
[self defaultErrorHandler:error];
}
}
}
+ (void) setErrorHandlerTarget:(id)target action:(SEL)action
{
errorHandlerTarget = target; /* Deliberately don't retain to avoid potential retain cycles */
errorHandlerAction = action;
}
- (void) handleErrors:(NSError *)error
{
[[self class] handleErrors:error];
}
+ (void) setupCoreDataStack
{
NSManagedObjectContext *context = [NSManagedObjectContext context];
[NSManagedObjectContext setDefaultContext:context];
}
+ (void) setupAutoMigratingCoreDataStack
{
[self setupCoreDataStackWithAutoMigratingSqliteStoreNamed:kActiveRecordDefaultStoreFileName];
}
+ (void) setupCoreDataStackWithStoreNamed:(NSString *)storeName
{
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator coordinatorWithSqliteStoreNamed:storeName];
[NSPersistentStoreCoordinator setDefaultStoreCoordinator:coordinator];
NSManagedObjectContext *context = [NSManagedObjectContext contextWithStoreCoordinator:coordinator];
[NSManagedObjectContext setDefaultContext:context];
}
+ (void) setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(NSString *)storeName
{
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator coordinatorWithAutoMigratingSqliteStoreNamed:storeName];
[NSPersistentStoreCoordinator setDefaultStoreCoordinator:coordinator];
NSManagedObjectContext *context = [NSManagedObjectContext contextWithStoreCoordinator:coordinator];
[NSManagedObjectContext setDefaultContext:context];
}
+ (void) setupCoreDataStackWithInMemoryStore
{
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator coordinatorWithInMemoryStore];
[NSPersistentStoreCoordinator setDefaultStoreCoordinator:coordinator];
NSManagedObjectContext *context = [NSManagedObjectContext contextWithStoreCoordinator:coordinator];
[NSManagedObjectContext setDefaultContext:context];
}
#ifdef NS_BLOCKS_AVAILABLE
#pragma mark DEPRECATED_METHOD
+ (void) performSaveDataOperationWithBlock:(CoreDataBlock)block;
{
[ARCoreDataAction saveDataWithBlock:block];
}
+ (void) performSaveDataOperationInBackgroundWithBlock:(CoreDataBlock)block;
{
[ARCoreDataAction saveDataWithBlock:block];
}
+ (void) performLookupOperationWithBlock:(CoreDataBlock)block;
{
[ARCoreDataAction lookupWithBlock:block];
}
+ (void) performSaveDataOperationInBackgroundWithBlock:(CoreDataBlock)block completion:(void(^)(void))callback;
{
[ARCoreDataAction saveDataInBackgroundWithBlock:block completion:callback];
}
#endif
@end