我正在尝试将UITableViewController子类化以最小化我的代码。
基本上我在超级类中的所有代码都免除了FetchedResultController,我在子类中重写它。
没有这个“拆分”但它现在返回EXC_BAD_ACCESS消息。
以下是我的子类中的代码:
- (NSFetchedResultsController *)fetchedResultsController
{
if (super.fetchedResultsController != nil)
{
return super.fetchedResultsController;
}
id delegate = [[UIApplication sharedApplication] delegate];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Question" inManagedObjectContext:[delegate managedObjectContext]];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor*sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"up_vote_count" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
//next line returns EXC_BAD_ACCESS
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[delegate managedObjectContext] sectionNameKeyPath:nil cacheName:@"Root2"];
aFetchedResultsController.delegate = self;
super.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return super.fetchedResultsController;
}
我很好奇我是否应该使用 super。或 self。但 self。在if语句中崩溃。
- 编辑 -
这是超类的@interface。除了覆盖的方法之外,我的子类不包含任何内容。
@interface CoreDataTableView : UITableViewController <NSFetchedResultsControllerDelegate> {
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@end
答案 0 :(得分:2)
自我。在if语句中崩溃了
这是因为你递归地将fetchedResultsController
方法调用为无限循环。
现在,还有一些其他问题。
首先,我建议将该方法分为两部分:
此外,您的fetchRequest
永远不会发布。也不是aFetchedResultsController
。您在该方法中拥有这两个对象,因此必须释放它们以防止泄漏。
根据这些评论,以下是代码的外观:
- (NSFetchedResultsController *)fetchedResultsController
{
// In this method, because it's overriding the accessor in the super-class,
// you must use |super.fetchedResultsController|,
// otherwise you will trigger an infinite loop!
if (super.fetchedResultsController != nil)
{
return super.fetchedResultsController;
}
id delegate = [[UIApplication sharedApplication] delegate];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// ...
// omitted the other details...
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[delegate managedObjectContext] sectionNameKeyPath:nil cacheName:@"Root2"];
aFetchedResultsController.delegate = self;
super.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
return super.fetchedResultsController;
}
- (void)fetch
{
NSError *error = nil;
// using |self| here is OK, because you're referring to the current object's method
if (![self.fetchedResultsController performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
至于使用super
vs self
:
super.foo
或[super foo]
开始寻找方法foo
超级self.foo
或[self foo]
在中查找方法foo
当前对象的类在你的情况下,除了self.fetchedResultsController
如前所述触发无限循环,如果在访问器内部使用时,它没有太大的区别。
我希望我能做到这一点并为你解决问题......