Skip to content

Instantly share code, notes, and snippets.

@AlanQuatermain
Created May 17, 2011 20:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlanQuatermain/977280 to your computer and use it in GitHub Desktop.
Save AlanQuatermain/977280 to your computer and use it in GitHub Desktop.
A fix for a bug in -countForFetchRequest: in iOS versions prior to 4.0. If you have multiple stores at the base of your CoreData stack, it would throw an exception. The fix below swaps out the faulty version with one which directs the request at the corre
@interface NSManagedObjectContext (AQFixedCountForFetchRequestOnIOS32)
@end
@implementation NSManagedObjectContext (AQFixedCountForFetchRequestOnIOS32)
+ (void) load
{
// +load gets called per-category, unlike +initialize
if ( [[[UIDevice currentDevice] systemVersion] compare: @"4.0" options: NSNumericSearch] == NSOrderedAscending )
{
// swap the broken method with our fixed version
Method brokenMethod = class_getInstanceMethod(self, @selector(countForFetchRequest:error:));
Method fixedMethod = class_getInstanceMethod(self, @selector(ipad32_fixed_countForFetchRequest:error:));
method_exchangeImplementations(brokenMethod, fixedMethod);
}
}
- (NSUInteger) ipad32_fixed_countForFetchRequest: (NSFetchRequest *) request error: (NSError **) error
{
NSManagedObjectModel * model = [[self persistentStoreCoordinator] managedObjectModel];
for ( NSPersistentStore * store in [[self persistentStoreCoordinator] persistentStores] )
{
if ( [[model entitiesForConfiguration: [store configurationName]] containsObject: [request entity]] )
{
[request setAffectedStores: [NSArray arrayWithObject: store]];
break;
}
}
// remember: the implementation was *swapped*
return ( [self ipad32_fixed_countForFetchRequest: request error: error] );
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment