Skip to content

Instantly share code, notes, and snippets.

@mbbischoff
Last active December 18, 2015 06:09
Show Gist options
  • Save mbbischoff/5737621 to your computer and use it in GitHub Desktop.
Save mbbischoff/5737621 to your computer and use it in GitHub Desktop.
// Run this in the viewDidLoad of a new Core Data project with the Master-Detail Application Template
// Replace the file name with your momd's filename.
- (void)testCoreDataEquality {
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TestCoreDataISEqual" withExtension:@"momd"];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
[storeCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:nil];
NSManagedObjectContext *mainQueueContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
mainQueueContext.persistentStoreCoordinator = storeCoordinator;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:mainQueueContext];
NSManagedObject *mainQueueObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:mainQueueContext];
NSManagedObjectContext *backgroundContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
backgroundContext.persistentStoreCoordinator = storeCoordinator;
__block NSManagedObject *backgroundObject;
[backgroundContext performBlockAndWait:^{
backgroundObject = [backgroundContext objectWithID:mainQueueObject.objectID];
}];
if ([mainQueueObject.objectID isEqual:backgroundObject.objectID]) {
NSLog(@"The objectIDs are equal");
if ([mainQueueObject isEqual:backgroundObject]) {
NSLog(@"Core Data believes the objects are equal");
}
else {
NSLog(@"Core Data believes the objects are not equal");
}
}
else {
NSLog(@"The objectIDs aren't equal");
}
}
@mbbischoff
Copy link
Author

Prints out:

2013-06-09 00:18:41.048 TestCoreDataISEqual[88449:c07] The objectIDs are equal
2013-06-09 00:18:41.049 TestCoreDataISEqual[88449:c07] Core Data believes the objects are not equal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment