forked from magicalpanda/MagicalRecord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActiveRecordHelpers.m
125 lines (105 loc) · 3.64 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
//
// ActiveRecordHelpers.m
//
// Created by Saul Mora on 3/11/10.
// Copyright 2010 Magical Panda Software, LLC All rights reserved.
//
#import "ActiveRecordHelpers.h"
#import "NSManagedObjectContext+ActiveRecord.h"
#import "NSPersistentStoreCoordinator+ActiveRecord.h"
#import "NSManagedObjectModel+ActiveRecord.h"
#import "NSPersistentStore+ActiveRecord.h"
#import <dispatch/dispatch.h>
@implementation ActiveRecordHelpers
+ (void) cleanUp
{
[NSManagedObjectContext setDefaultContext:nil];
[NSManagedObjectModel setDefaultManagedObjectModel:nil];
[NSPersistentStoreCoordinator setDefaultStoreCoordinator:nil];
[NSPersistentStore setDetaultPersistentStore:nil];
}
+ (void) handleErrors:(NSError *)error
{
if (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)])
{
NSLog(@"Error Details: %@", [e userInfo]);
}
else
{
NSLog(@"Error Details: %@", e);
}
}
}
else
{
NSLog(@"Error: %@", detailedError);
}
}
NSLog(@"Error Domain: %@", [error domain]);
NSLog(@"Recovery Suggestion: %@", [error localizedRecoverySuggestion]);
}
}
- (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
+ (void) performSaveDataOperationWithBlock:(CoreDataBlock)block
{
NSManagedObjectContext *localContext = [NSManagedObjectContext contextThatNotifiesDefaultContextOnMainThread];
[localContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
block(localContext);
if ([localContext hasChanges])
{
[localContext save];
}
[[NSManagedObjectContext defaultContext] stopObservingContext:localContext];
}
+ (void) performSaveDataOperationInBackgroundWithBlock:(CoreDataBlock)block
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
[self performSaveDataOperationWithBlock:block];
});
}
#endif
@end