magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.8k stars 1.79k forks source link

executeFetchRequest:error: A fetch request must have an entity. #1312

Open JustinYangJing opened 6 years ago

JustinYangJing commented 6 years ago

MagicalRecord Version: 2.3.2 Xcode:8.3.3 Crash iOS Version :8.x ,9.x Crash Reason: A fetch request without entity name .

Entity name is generated by the following function when MagicalRecord alloc a fetch request.

there is no 'fetchRequest' function if the class (a subclass of NSManagedObject) was generated by Xcode 7.x.

So I fix this bug by hooking MR_entityName function . create a NSManagedObject category. the following is the code .

+(void)load{ Method m1 = class_getClassMethod([self class], @selector(MR_entityName)); Method m2 = class_getClassMethod([self class], @selector(MR_entityNameHook)); method_exchangeImplementations(m1, m2); } +(NSString )MR_entityNameHook{ if ([self respondsToSelector:@selector(fetchRequest)]) { NSFetchRequest request = [self fetchRequest]; return request.entityName; } return [self MR_entityNameHook]; }