Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
Added an NSManagedObjectContext extension to easily create a child co…
Browse files Browse the repository at this point in the history
…ntext
  • Loading branch information
jspahrsummers committed Apr 12, 2012
1 parent c4c3f1c commit 60d93e8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Proton/NSManagedObjectContext+ConvenienceAdditions.h
Expand Up @@ -13,6 +13,19 @@
*/
@interface NSManagedObjectContext (ConvenienceAdditions)

/**
* @name Creating Child Contexts
*/

/**
* Creates and returns a managed object context with its `parentContext` set to
* the receiver.
*
* The returned context will be initialized with `NSConfinementConcurrencyType`,
* and will not have an undo manager by default (even on Mac OS X).
*/
- (NSManagedObjectContext *)newChildContext;

/**
* @name Refreshing Objects
*/
Expand Down
9 changes: 9 additions & 0 deletions Proton/NSManagedObjectContext+ConvenienceAdditions.m
Expand Up @@ -11,6 +11,15 @@
#import "EXTScope.h"

@safecategory (NSManagedObjectContext, ConvenienceAdditions)
- (NSManagedObjectContext *)newChildContext; {
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];

context.parentContext = self;
context.undoManager = nil;

return context;
}

- (void)refreshAllObjectsMergingChanges:(BOOL)mergeChanges; {
NSSet *objects = [self.registeredObjects copy];

Expand Down
12 changes: 12 additions & 0 deletions ProtonTests/PRONSManagedObjectContextAdditionsTests.m
Expand Up @@ -161,4 +161,16 @@
expect(undoManager.canUndo).toBeFalsy();
});

it(@"should return a child context", ^{
NSManagedObjectContext *childContext = [context newChildContext];
expect(childContext).not.toBeNil();
expect(childContext.parentContext).toEqual(context);
expect(childContext.concurrencyType).toEqual(NSConfinementConcurrencyType);
expect(childContext.undoManager).toBeNil();
});

it(@"should return unique child contexts", ^{
expect([context newChildContext]).not.toEqual([context newChildContext]);
});

SpecEnd

0 comments on commit 60d93e8

Please sign in to comment.