Permalink
Browse files

Tweak to MR_inContext to allow for items to be found in a child contexts

  • Loading branch information...
1 parent 1d9e0db commit 29329f7fa8825eacb4cf94f07f090754c022680a @blackgold9 committed Aug 2, 2012
@@ -221,10 +221,24 @@ + (BOOL) MR_truncateAll
- (id) MR_inContext:(NSManagedObjectContext *)otherContext
{
- NSError *error = nil;
+ __block NSError *error;
+ if ([self.objectID isTemporaryID]) {
+ if (otherContext.parentContext == self.managedObjectContext)
+ {
+ // We have a temporary id, which can never be used to lookup accross contexts, but we're trying it from a child context, so we can just get it
+ [self.managedObjectContext performBlockAndWait:^{
+ MRLog(@"Automatically obtaining a permanent Id so we can find it in a child context");
+ [self.managedObjectContext obtainPermanentIDsForObjects:[NSArray arrayWithObject:self] error:nil];
+ [MagicalRecord handleErrors:error];
+ error = nil;
+ }];
+ } else {
+ MRLog(@"Unable to obtain a permanent Id for object. [NSManagedObject MR_inContext:] will not succeed");
+ }
+ }
+
NSManagedObject *inContext = [otherContext existingObjectWithID:[self objectID] error:&error];
[MagicalRecord handleErrors:error];
-
return inContext;
}
@@ -7,20 +7,26 @@
//
#import "NSManagedObjectContextHelperTests.h"
+#import "SingleRelatedEntity.h"
@implementation NSManagedObjectContextHelperTests
+- (void) setUpClass
+{
+ [NSManagedObjectModel MR_setDefaultManagedObjectModel:[NSManagedObjectModel MR_managedObjectModelNamed:@"TestModel.momd"]];
+}
+
- (void) setUp
{
[MagicalRecord setupCoreDataStackWithInMemoryStore];
}
- (void) tearDown
{
- [MagicalRecord cleanUp];
+ //[MagicalRecord cleanUp];
}
-- (void) testCanCreateContextForCurrentThead
+- (void) testCanCreateContextForCurrentThread
{
NSManagedObjectContext *firstContext = [NSManagedObjectContext MR_contextForCurrentThread];
NSManagedObjectContext *secondContext = [NSManagedObjectContext MR_contextForCurrentThread];
@@ -31,8 +37,40 @@ - (void) testCanCreateContextForCurrentThead
- (void) testCanNotifyDefaultContextOnSave
{
NSManagedObjectContext *testContext = [NSManagedObjectContext MR_contextThatPushesChangesToDefaultContext];
+
+ assertThat([testContext parentContext], is(equalTo([NSManagedObjectContext MR_defaultContext])));
+}
- assertThat([testContext parentContext], is(equalTo([NSManagedObjectContext MR_defaultContext])));
+- (void)testBustedNestedContextLookup
+{
+ dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
+ dispatch_async(dispatch_get_main_queue(), ^{
+
+ assertThat([NSManagedObjectContext MR_defaultContext], is(notNilValue()));
+
+ NSManagedObjectContext *mainContext = [NSManagedObjectContext MR_defaultContext];
+ // Create a new object
+ SingleRelatedEntity *entity = [SingleRelatedEntity createInContext:[NSManagedObjectContext MR_defaultContext]];
+ entity.mappedStringAttribute = @"test";
+ assertThat(entity, isIn([mainContext insertedObjects]));
+
+ // Find it in the background, to do stuff to it or something
+ [MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext) {
+ SingleRelatedEntity *localEntity = [entity MR_inContext:localContext];
+ assertThat(localEntity, is(notNilValue()));
+ localEntity.mappedStringAttribute = @"Test";
+
+ } completion:^{
+ dispatch_semaphore_signal(semaphore);
+ NSLog(@"Got ehre");
+ }];
+
+ });
+
+ dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_FOREVER, 0));
+ NSLog(@"Done");
+
+
}

0 comments on commit 29329f7

Please sign in to comment.