Skip to content

Commit

Permalink
Fix crashes during test suite execution due to delay during processin…
Browse files Browse the repository at this point in the history
…g of NSManagedObjectContextDidSaveNotification
  • Loading branch information
blakewatters committed Aug 7, 2012
1 parent 99738db commit 6cc54fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
1 change: 1 addition & 0 deletions Code/CoreData/RKManagedObjectStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ - (void)handlePrimaryManagedObjectContextDidSaveNotification:(NSNotification *)n
{
RKLogDebug(@"primaryManagedObjectContext was saved: merging changes to mainQueueManagedObjectContext");
RKLogTrace(@"Merging changes detailed in userInfo dictionary: %@", [notification userInfo]);
NSAssert([notification object] == self.primaryManagedObjectContext, @"Received Managed Object Context Did Save Notification for Unexpected Context: %@", [notification object]);
[self.mainQueueManagedObjectContext performBlock:^{
[self.mainQueueManagedObjectContext mergeChangesFromContextDidSaveNotification:notification];
}];
Expand Down
50 changes: 24 additions & 26 deletions Tests/Logic/CoreData/RKManagedObjectStoreTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ @implementation RKManagedObjectStoreTest

- (void)setUp
{
[RKTestFactory setUp];

// Delete any sqlite files in the app data directory
NSError *error;
NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[RKDirectory applicationDataDirectory] error:&error];
Expand All @@ -42,32 +44,17 @@ - (void)setUp
}
}

//- (void)testInstantiationOfNewManagedObjectContextAssociatesWithObjectStore
//{
// RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
// NSManagedObjectContext *context = [managedObjectStore newChildManagedObjectContextWithConcurrencyType:NSMainQueueConcurrencyType];
// assertThat([context managedObjectStore], is(equalTo(managedObjectStore)));
//}
//
//- (void)testCreationOfStoreInSpecificDirectoryRaisesIfDoesNotExist
//{
// NSString *path = [[RKDirectory applicationDataDirectory] stringByAppendingPathComponent:@"/NonexistantSubdirectory"];
// BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path];
// assertThatBool(exists, is(equalToBool(NO)));
// STAssertThrows([RKManagedObjectStore objectStoreWithStoreFilename:@"Whatever.sqlite" inDirectory:path usingSeedDatabaseName:nil managedObjectModel:nil delegate:nil], nil);
//}
//
//- (void)testCreationOfStoryInApplicationDirectoryCreatesIfNonExistant
//{
// // On OS X, the application directory is not created for you
// NSString *path = [RKDirectory applicationDataDirectory];
// NSError *error = nil;
// [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
// assertThat(error, is(nilValue()));
// STAssertNoThrow([RKManagedObjectStore objectStoreWithStoreFilename:@"Whatever.sqlite" inDirectory:nil usingSeedDatabaseName:nil managedObjectModel:nil delegate:nil], nil);
// BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path];
// assertThatBool(exists, is(equalToBool(YES)));
//}
- (void)tearDown
{
[RKTestFactory tearDown];
}

- (void)testInstantiationOfNewManagedObjectContextAssociatesWithObjectStore
{
RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
NSManagedObjectContext *context = [managedObjectStore newChildManagedObjectContextWithConcurrencyType:NSMainQueueConcurrencyType];
assertThat([context managedObjectStore], is(equalTo(managedObjectStore)));
}

- (void)testAdditionOfSQLiteStoreRetainsPathOfSeedDatabase
{
Expand Down Expand Up @@ -126,6 +113,7 @@ - (void)testAddingPersistentSQLiteStoreFromSeedDatabase
assertThat(array, isNot(empty()));
RKHuman *seededHuman = [array objectAtIndex:0];
assertThat([[seededHuman.objectID URIRepresentation] URLByDeletingLastPathComponent], is(equalTo([[seedObjectID URIRepresentation] URLByDeletingLastPathComponent])));
[seededStore release];
}

- (void)testResetPersistentStoresRecreatesInMemoryStoreThusDeletingAllManagedObjects
Expand All @@ -139,6 +127,9 @@ - (void)testResetPersistentStoresRecreatesInMemoryStoreThusDeletingAllManagedObj
human.name = @"Blake";
BOOL success = [managedObjectStore.mainQueueManagedObjectContext saveToPersistentStore:&error];
assertThatBool(success, is(equalToBool(YES)));

// Spin the run loop to allow the did save notifications to propogate
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];

success = [managedObjectStore resetPersistentStores:&error];
assertThatBool(success, is(equalToBool(YES)));
Expand All @@ -152,6 +143,7 @@ - (void)testResetPersistentStoresRecreatesInMemoryStoreThusDeletingAllManagedObj
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"name = %@", @"Blake"];
NSArray *array = [managedObjectStore.mainQueueManagedObjectContext executeFetchRequest:fetchRequest error:&error];
assertThat(array, is(empty()));
[managedObjectStore release];
}

- (void)testResetPersistentStoresRecreatesSQLiteStoreThusDeletingAllManagedObjects
Expand All @@ -167,6 +159,9 @@ - (void)testResetPersistentStoresRecreatesSQLiteStoreThusDeletingAllManagedObjec
BOOL success = [managedObjectStore.mainQueueManagedObjectContext saveToPersistentStore:&error];
assertThatBool(success, is(equalToBool(YES)));

// Spin the run loop to allow the did save notifications to propogate
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];

success = [managedObjectStore resetPersistentStores:&error];
assertThatBool(success, is(equalToBool(YES)));

Expand All @@ -175,6 +170,7 @@ - (void)testResetPersistentStoresRecreatesSQLiteStoreThusDeletingAllManagedObjec
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"name = %@", @"Blake"];
NSArray *array = [managedObjectStore.mainQueueManagedObjectContext executeFetchRequest:fetchRequest error:&error];
assertThat(array, is(empty()));
[managedObjectStore release];
}


Expand Down Expand Up @@ -204,6 +200,7 @@ - (void)testResetPersistentStoreRecreatesSQLiteStoreThusRecreatingTheStoreFileOn

NSDate *laterDate = [modificationDate laterDate:newModificationDate];
assertThat(laterDate, is(equalTo(newModificationDate)));
[managedObjectStore release];
}

- (void)testResetPersistentStoreForSQLiteStoreSeededWithDatabaseReclonesTheSeedDatabaseToTheStoreLocation
Expand Down Expand Up @@ -252,6 +249,7 @@ - (void)testResetPersistentStoreForSQLiteStoreSeededWithDatabaseReclonesTheSeedD
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"name = %@", @"Sarah"];
array = [seededStore.primaryManagedObjectContext executeFetchRequest:fetchRequest error:&error];
assertThat(array, is(empty()));
[seededStore release];
}

@end

0 comments on commit 6cc54fa

Please sign in to comment.