/
MagicalRecordNestedContextsPersistenceStrategy.m
31 lines (27 loc) · 1.39 KB
/
MagicalRecordNestedContextsPersistenceStrategy.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
//
// MagicalRecordNestedContextsPersistenceStrategy.m
// Magical Record
//
// Created by Stephen J Vanterpool on 8/31/12.
// Copyright (c) 2012 Magical Panda Software LLC. All rights reserved.
//
#import "MagicalRecordNestedContextsPersistenceStrategy.h"
#import "NSManagedObjectContext+MagicalRecordInternal.h"
@implementation MagicalRecordNestedContextsPersistenceStrategy
- (void)setUpContextsWithCoordinator:(NSPersistentStoreCoordinator *)coordinator {
MRLog(@"Wohoo! Running iOS 6, using nested contexts!");
NSManagedObjectContext *rootContext = [NSManagedObjectContext MR_contextWithStoreCoordinator:coordinator];
[NSManagedObjectContext MR_setRootSavingContext:rootContext];
NSManagedObjectContext *defaultContext = [NSManagedObjectContext MR_newMainQueueContext];
[defaultContext setParentContext:rootContext];
[NSManagedObjectContext MR_setDefaultContext:defaultContext];
[NSManagedObjectContext MR_makeContextObtainPermanentIDsBeforeSaving:defaultContext];
[NSManagedObjectContext MR_makeContextObtainPermanentIDsBeforeSaving:rootContext];
[NSManagedObjectContext MR_makeContext:rootContext mergeChangesToContext:defaultContext];
}
- (NSManagedObjectContext *)contextToUseForBackgroundSaves
{
NSManagedObjectContext *mainContext = [NSManagedObjectContext MR_rootSavingContext];
return [NSManagedObjectContext MR_contextWithParent:mainContext];
}
@end