Skip to content

Commit 6e14f3b

Browse files
committed
refactored differences into persistence strategy class
1 parent 058ea0b commit 6e14f3b

15 files changed

+192
-104
lines changed

MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalRecord.m

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,11 @@
66
//
77

88
#import "CoreData+MagicalRecord.h"
9+
#import "MagicalRecordPersistenceStrategy.h"
910
#import <objc/runtime.h>
1011

1112
static NSManagedObjectContext *rootSavingContext = nil;
1213
static NSManagedObjectContext *defaultManagedObjectContext_ = nil;
13-
static NSManagedObjectContext *backgroundWorkContext = nil;
14-
15-
16-
@interface NSManagedObjectContext (MagicalRecordInternal)
17-
18-
- (void) MR_mergeChangesFromNotification:(NSNotification *)notification;
19-
- (void) MR_mergeChangesOnMainThread:(NSNotification *)notification;
20-
+ (void) MR_setDefaultContext:(NSManagedObjectContext *)moc;
21-
+ (void) MR_setRootSavingContext:(NSManagedObjectContext *)context;
22-
23-
@end
24-
2514

2615
@implementation NSManagedObjectContext (MagicalRecord)
2716

@@ -79,37 +68,7 @@ + (void) MR_setRootSavingContext:(NSManagedObjectContext *)context;
7968

8069
+ (void) MR_initializeDefaultContextWithCoordinator:(NSPersistentStoreCoordinator *)coordinator;
8170
{
82-
if (defaultManagedObjectContext_ == nil)
83-
{
84-
if ([MagicalRecord isRunningiOS6]) {
85-
MRLog(@"Wohoo! Running iOS 6, using nested contexts!");
86-
NSManagedObjectContext *rootContext = [self MR_contextWithStoreCoordinator:coordinator];
87-
88-
[self MR_setRootSavingContext:rootContext];
89-
90-
NSManagedObjectContext *defaultContext = [self MR_newMainQueueContext];
91-
[defaultContext setParentContext:rootSavingContext];
92-
93-
[self MR_setDefaultContext:defaultContext];
94-
} else {
95-
MRLog(@"On iOS 5, nested contexts are trouble. Using parallel PSC's!");
96-
// We can't do nested contexts, so let's create a seperate psc and merge from there to main thread context
97-
98-
NSPersistentStoreCoordinator *backgroundCoordinator = [NSPersistentStoreCoordinator MR_coordinatorWithAutoMigratingSqliteStoreNamed:[MagicalRecord defaultStoreName]];
99-
NSManagedObjectContext *rootContext = [self MR_contextWithStoreCoordinator:backgroundCoordinator];
100-
[self MR_setRootSavingContext:rootContext];
101-
rootContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy;
102-
// Now set up the UIQueue
103-
NSManagedObjectContext *defaultContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
104-
defaultContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy;
105-
// Use the coordinator passed in
106-
[defaultContext setPersistentStoreCoordinator:coordinator];
107-
[self MR_setDefaultContext:defaultContext];
108-
109-
[self MR_makeContext:rootContext mergeChangesToContext:defaultContext];
110-
[self MR_makeContext:defaultContext mergeChangesToContext:rootContext];
111-
}
112-
}
71+
[[MagicalRecord persistenceStrategy] setUpContextsWithCoordinator:coordinator];
11372
}
11473

11574
+ (void)MR_makeContext:(NSManagedObjectContext *)sourceContext mergeChangesToContext:(NSManagedObjectContext *)targetContext
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#import <Foundation/Foundation.h>
2+
3+
4+
@interface NSManagedObjectContext (MagicalRecordInternal)
5+
6+
- (void) MR_mergeChangesFromNotification:(NSNotification *)notification;
7+
- (void) MR_mergeChangesOnMainThread:(NSNotification *)notification;
8+
+ (void) MR_setDefaultContext:(NSManagedObjectContext *)moc;
9+
+ (void) MR_setRootSavingContext:(NSManagedObjectContext *)context;
10+
+ (void)MR_makeContextObtainPermanentIDsBeforeSaving:(NSManagedObjectContext *)context;
11+
+ (void)MR_makeContext:(NSManagedObjectContext *)sourceContext mergeChangesToContext:(NSManagedObjectContext *)targetContext;
12+
@end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// Created by svanter on 8/31/12.
3+
//
4+
// To change the template use AppCode | Preferences | File Templates.
5+
//
6+
7+
8+
#import "NSManagedObjectContext+MagicalRecordInternal.h"
9+
10+
11+
@implementation NSManagedObjectContext (MagicalRecordInternal)
12+
@end

MagicalRecord/Core/MagicalRecord+Actions.m

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import "CoreData+MagicalRecord.h"
1010
#import "NSManagedObjectContext+MagicalRecord.h"
11+
#import "MagicalRecordPersistenceStrategy.h"
1112

1213
static dispatch_queue_t background_action_queue;
1314

@@ -35,49 +36,38 @@ @implementation MagicalRecord (Actions)
3536

3637
+ (void) saveInBackgroundUsingContext:(NSManagedObjectContext *)localContext block:(void (^)(NSManagedObjectContext *))block completion:(void(^)(void))completion errorHandler:(void(^)(NSError *))errorHandler;
3738
{
38-
39-
// dispatch_async(action_queue(), ^{
40-
// block(localContext);
41-
//
42-
// [localContext MR_saveInBackgroundErrorHandler:errorHandler completion:completion];
43-
// });
44-
39+
dispatch_group_t completionGroup = dispatch_group_create();
4540
[localContext performBlock:^{
4641
block(localContext);
4742
// Save the context we were given
4843
[localContext MR_saveErrorHandler:nil];
4944
if (localContext.parentContext) {
5045
// If we're doing nested contexs, save parent
46+
dispatch_group_enter(completionGroup);
5147
[localContext.parentContext performBlock:^{
5248
[localContext.parentContext MR_saveErrorHandler:nil];
53-
if (completion) {
54-
dispatch_async(dispatch_get_main_queue(), ^{
55-
completion();
56-
});
57-
}
49+
dispatch_group_leave(completionGroup);
5850
}];
59-
} else {
60-
// we're not, so just call completion;
51+
}
52+
53+
// If the context has a parent context, this code will execute after the save.
54+
// If not, it will execute immediately
55+
dispatch_group_notify(completionGroup, dispatch_get_main_queue(), ^{
6156
if (completion) {
6257
dispatch_async(dispatch_get_main_queue(), ^{
6358
completion();
6459
});
6560
}
66-
}
61+
62+
dispatch_release(completionGroup);
63+
});
6764
}];
6865

6966
}
7067

7168
+ (void) saveInBackgroundWithBlock:(void (^)(NSManagedObjectContext *))block completion:(void (^)(void))completion errorHandler:(void (^)(NSError *))errorHandler;
7269
{
73-
NSManagedObjectContext *savingContext;
74-
if ([MagicalRecord isRunningiOS6]) {
75-
NSManagedObjectContext *mainContext = [NSManagedObjectContext MR_rootSavingContext];
76-
savingContext = [NSManagedObjectContext MR_contextWithParent:mainContext];
77-
} else {
78-
savingContext = [NSManagedObjectContext MR_rootSavingContext];
79-
}
80-
70+
NSManagedObjectContext *savingContext = [[MagicalRecord persistenceStrategy] contextToUseForBackgroundSaves];
8171
[self saveInBackgroundUsingContext:savingContext block:block completion:completion errorHandler:errorHandler];
8272
}
8373

MagicalRecord/Core/MagicalRecord+Options.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#import "MagicalRecord.h"
1010

11+
@protocol MagicalRecordPersistenceStrategy;
12+
1113
@interface MagicalRecord (Options)
1214

1315
//global options
@@ -30,4 +32,7 @@
3032
+ (BOOL) shouldDeleteStoreOnModelMismatch;
3133
+ (BOOL)isRunningiOS6;
3234

35+
+ (id <MagicalRecordPersistenceStrategy>)persistenceStrategy;
36+
37+
3338
@end

MagicalRecord/Core/MagicalRecord+Options.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
//
88

99
#import "MagicalRecord+Options.h"
10+
#import "MagicalRecordPersistenceStrategy.h"
11+
#import "MagicalRecordNestedContextsPersistenceStrategy.h"
12+
#import "MagicalRecordParallelStoresPersistenceStrategy.h"
1013

1114
static BOOL shouldAutoCreateManagedObjectModel_;
1215
static BOOL shouldAutoCreateDefaultPersistentStoreCoordinator_;
@@ -51,4 +54,19 @@ + (BOOL)isRunningiOS6
5154
return NSClassFromString(@"NSUUID") != nil;
5255
}
5356

57+
+ (id<MagicalRecordPersistenceStrategy>)persistenceStrategy
58+
{
59+
static id<MagicalRecordPersistenceStrategy>strategy;
60+
61+
static dispatch_once_t once;
62+
dispatch_once(&once, ^{
63+
if ([MagicalRecord isRunningiOS6]) {
64+
strategy= (id<MagicalRecordPersistenceStrategy>)[[MagicalRecordNestedContextsPersistenceStrategy alloc] init];
65+
} else {
66+
strategy = (id<MagicalRecordPersistenceStrategy>)[[MagicalRecordParallelStoresPersistenceStrategy alloc] init];
67+
}
68+
});
69+
70+
return strategy;
71+
}
5472
@end

MagicalRecord/Core/MagicalRecord+Setup.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "NSManagedObject+MagicalRecord.h"
1111
#import "NSPersistentStoreCoordinator+MagicalRecord.h"
1212
#import "NSManagedObjectContext+MagicalRecord.h"
13+
#import "MagicalRecordPersistenceStrategy.h"
1314

1415
@implementation MagicalRecord (Setup)
1516

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// MagicalRecordNestedContextsPersistenceStrategy.h
3+
// Magical Record
4+
//
5+
// Created by Stephen J Vanterpool on 8/31/12.
6+
// Copyright (c) 2012 Magical Panda Software LLC. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "MagicalRecordPersistenceStrategy.h"
11+
12+
@interface MagicalRecordNestedContextsPersistenceStrategy : NSObject<MagicalRecordPersistenceStrategy>
13+
14+
@end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// MagicalRecordNestedContextsPersistenceStrategy.m
3+
// Magical Record
4+
//
5+
// Created by Stephen J Vanterpool on 8/31/12.
6+
// Copyright (c) 2012 Magical Panda Software LLC. All rights reserved.
7+
//
8+
9+
#import "MagicalRecordNestedContextsPersistenceStrategy.h"
10+
#import "NSManagedObjectContext+MagicalRecordInternal.h"
11+
12+
@implementation MagicalRecordNestedContextsPersistenceStrategy
13+
- (void)setUpContextsWithCoordinator:(NSPersistentStoreCoordinator *)coordinator {
14+
MRLog(@"Wohoo! Running iOS 6, using nested contexts!");
15+
NSManagedObjectContext *rootContext = [NSManagedObjectContext MR_contextWithStoreCoordinator:coordinator];
16+
[NSManagedObjectContext MR_setRootSavingContext:rootContext];
17+
NSManagedObjectContext *defaultContext = [NSManagedObjectContext MR_newMainQueueContext];
18+
[defaultContext setParentContext:rootContext];
19+
[NSManagedObjectContext MR_setDefaultContext:defaultContext];
20+
}
21+
22+
- (NSManagedObjectContext *)contextToUseForBackgroundSaves
23+
{
24+
NSManagedObjectContext *mainContext = [NSManagedObjectContext MR_rootSavingContext];
25+
return [NSManagedObjectContext MR_contextWithParent:mainContext];
26+
}
27+
28+
@end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#import <Foundation/Foundation.h>
2+
3+
4+
@interface MagicalRecordParallelStoresPersistenceStrategy : NSObject
5+
@end

0 commit comments

Comments
 (0)