Created
May 17, 2011 20:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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