Skip to content

Commit 5915f5c

Browse files
committed
Merge pull request magicalpanda#249 from blackgold9/ios5Fixes
Ios5 fixes
2 parents 974c257 + b2bc56b commit 5915f5c

28 files changed

+1100
-36
lines changed

MagicalRecord.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Pod::Spec.new do |s|
55
s.summary = 'Super Awesome Easy Fetching for Core Data 1!!!11!!!!1! '
66
s.homepage = 'http://github.com/magicalpanda/MagicalRecord'
77
s.author = { 'Saul Mora' => 'saul@magicalpanda.com' }
8-
s.source = { :git => 'http://github.com/magicalpanda/MagicalRecord.git', :tag => '2.0' }
8+
s.source = { :git => 'http://github.com/blackgold9/MagicalRecord.git', :commit =>'6a45944c0fecbe6d93d3d6febaacab1bbecda71c' }
99
s.description = 'Handy fetching, threading and data import helpers to make Core Data a little easier to use.'
1010
s.source_files = 'MagicalRecord/**/*.{h,m}'
1111
s.framework = 'CoreData'

MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalRecord.m

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,12 @@
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;
1314

14-
15-
@interface NSManagedObjectContext (MagicalRecordInternal)
16-
17-
- (void) MR_mergeChangesFromNotification:(NSNotification *)notification;
18-
- (void) MR_mergeChangesOnMainThread:(NSNotification *)notification;
19-
+ (void) MR_setDefaultContext:(NSManagedObjectContext *)moc;
20-
+ (void) MR_setRootSavingContext:(NSManagedObjectContext *)context;
21-
22-
@end
23-
24-
2515
@implementation NSManagedObjectContext (MagicalRecord)
2616

2717
+ (void) MR_cleanUp;
@@ -78,19 +68,39 @@ + (void) MR_setRootSavingContext:(NSManagedObjectContext *)context;
7868

7969
+ (void) MR_initializeDefaultContextWithCoordinator:(NSPersistentStoreCoordinator *)coordinator;
8070
{
81-
if (defaultManagedObjectContext_ == nil)
82-
{
83-
NSManagedObjectContext *rootContext = [self MR_contextWithStoreCoordinator:coordinator];
84-
85-
[self MR_setRootSavingContext:rootContext];
86-
87-
NSManagedObjectContext *defaultContext = [self MR_newMainQueueContext];
88-
[defaultContext setParentContext:rootSavingContext];
71+
[[MagicalRecord persistenceStrategy] setUpContextsWithCoordinator:coordinator];
72+
}
8973

90-
[self MR_setDefaultContext:defaultContext];
91-
}
74+
+ (void)MR_makeContext:(NSManagedObjectContext *)sourceContext mergeChangesToContext:(NSManagedObjectContext *)targetContext
75+
{
76+
[[NSNotificationCenter defaultCenter] addObserverForName:NSManagedObjectContextDidSaveNotification
77+
object:sourceContext
78+
queue:nil
79+
usingBlock:^(NSNotification *note) {
80+
NSAssert(note.object != nil, nil);
81+
NSAssert(targetContext!= nil, nil);
82+
83+
[targetContext performBlock:^{
84+
[targetContext mergeChangesFromContextDidSaveNotification:note];
85+
}];
86+
}];
9287
}
9388

89+
+ (void)MR_makeContextObtainPermanentIDsBeforeSaving:(NSManagedObjectContext *)context
90+
{
91+
[[NSNotificationCenter defaultCenter] addObserverForName:NSManagedObjectContextWillSaveNotification
92+
object:context
93+
queue:nil
94+
usingBlock:^(NSNotification *note) {
95+
[context performBlockAndWait:^{
96+
NSArray *insertedObjects = [[context insertedObjects] allObjects];
97+
NSError *error;
98+
if (![context obtainPermanentIDsForObjects:insertedObjects error:&error]) {
99+
[MagicalRecord handleErrors:error];
100+
}
101+
}];
102+
}];
103+
}
94104
+ (void) MR_resetDefaultContext
95105
{
96106
void (^resetBlock)(void) = ^{
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/Categories/NSPersistentStoreCoordinator+MagicalRecord.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ + (NSDictionary *) MR_autoMigrationOptions;
122122
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
123123
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
124124
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
125+
@{@"journal_mode" :@"WAL"}, NSSQLitePragmasOption,
125126
nil];
126127
return options;
127128
}

MagicalRecord/Core/MagicalRecord+Actions.m

Lines changed: 30 additions & 9 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,19 +36,39 @@ @implementation MagicalRecord (Actions)
3536

3637
+ (void) saveInBackgroundUsingContext:(NSManagedObjectContext *)localContext block:(void (^)(NSManagedObjectContext *))block completion:(void(^)(void))completion errorHandler:(void(^)(NSError *))errorHandler;
3738
{
38-
dispatch_async(action_queue(), ^{
39-
block(localContext);
40-
41-
[localContext MR_saveInBackgroundErrorHandler:errorHandler completion:completion];
42-
});
39+
dispatch_group_t completionGroup = dispatch_group_create();
40+
[localContext performBlock:^{
41+
block(localContext);
42+
// Save the context we were given
43+
[localContext MR_saveErrorHandler:nil];
44+
if (localContext.parentContext) {
45+
// If we're doing nested contexs, save parent
46+
dispatch_group_enter(completionGroup);
47+
[localContext.parentContext performBlock:^{
48+
[localContext.parentContext MR_saveErrorHandler:nil];
49+
dispatch_group_leave(completionGroup);
50+
}];
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(), ^{
56+
if (completion) {
57+
dispatch_async(dispatch_get_main_queue(), ^{
58+
completion();
59+
});
60+
}
61+
62+
dispatch_release(completionGroup);
63+
});
64+
}];
65+
4366
}
4467

4568
+ (void) saveInBackgroundWithBlock:(void (^)(NSManagedObjectContext *))block completion:(void (^)(void))completion errorHandler:(void (^)(NSError *))errorHandler;
4669
{
47-
NSManagedObjectContext *mainContext = [NSManagedObjectContext MR_defaultContext];
48-
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextWithParent:mainContext];
49-
50-
[self saveInBackgroundUsingContext:localContext block:block completion:completion errorHandler:errorHandler];
70+
NSManagedObjectContext *savingContext = [[MagicalRecord persistenceStrategy] contextToUseForBackgroundSaves];
71+
[self saveInBackgroundUsingContext:savingContext block:block completion:completion errorHandler:errorHandler];
5172
}
5273

5374
+ (void) saveInBackgroundUsingCurrentContextWithBlock:(void (^)(NSManagedObjectContext *))block completion:(void (^)(void))completion errorHandler:(void (^)(NSError *))errorHandler;

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
@@ -28,6 +30,9 @@
2830
This is extremely useful during development where every model change could potentially require a delete/reinstall of the app.
2931
*/
3032
+ (BOOL) shouldDeleteStoreOnModelMismatch;
33+
+ (BOOL)isRunningiOS6;
34+
35+
+ (id <MagicalRecordPersistenceStrategy>)persistenceStrategy;
3136

3237

3338
@end

MagicalRecord/Core/MagicalRecord+Options.m

Lines changed: 23 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_;
@@ -46,4 +49,24 @@ + (void) setShouldDeleteStoreOnModelMismatch:(BOOL)shouldDeleteStoreOnModelMisma
4649
shouldDeleteStoreOnModelMismatch_ = shouldDeleteStoreOnModelMismatch;
4750
}
4851

52+
+ (BOOL)isRunningiOS6
53+
{
54+
return NSClassFromString(@"NSUUID") != nil;
55+
}
56+
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+
}
4972
@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

MagicalRecord/Core/MagicalRecord.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#if MR_ENABLE_ACTIVE_RECORD_LOGGING != 0
2424
// First, check if we can use Cocoalumberjack for logging
2525
#ifdef LOG_VERBOSE
26-
extern int ddLogLevel;
26+
// extern int ddLogLevel;
2727
#define MRLog(...) DDLogVerbose(__VA_ARGS__)
2828
#else
2929
#define MRLog(...) NSLog(@"%s(%p) %@", __PRETTY_FUNCTION__, self, [NSString stringWithFormat:__VA_ARGS__])

0 commit comments

Comments
 (0)