NSFetchedResultsController initWithFetchRequest 抛出 EXE-BAD-ACCESS 异常
我在 UINavigationController
内的 UITableViewController
子类中有一个 NSFetchedResultsController
。当我运行应用程序时,前三次访问视图(转到它,然后单击“返回”,然后再次转到它)一切正常,但在第四次(总是)它崩溃并显示以下内容:
-[NSEntityDescription subentitiesByName]:发送到已释放实例 0x8b09c80 的消息
任何帮助将不胜感激。
这是结果控制器的 getter:
- (NSFetchedResultsController*)eventsResultsController {
if (eventsResultsController_ == nil) {
NSFetchRequest *aFetchRequest = [[PADataContext sharedInstance] makeGetAllFetchRequestForEntity:@"PAEvent" sortedBy:@"when" ascending:NO];
// NOTE: crashes on this next line
NSFetchedResultsController *aFetchedResultsContorller = [[NSFetchedResultsController alloc] initWithFetchRequest:aFetchRequest managedObjectContext:[[PADataContext sharedInstance] managedObjectContext] sectionNameKeyPath:@"whenMonth" cacheName:@"AllEvent"];
self.eventsResultsController = aFetchedResultsContorller;
[aFetchedResultsContorller release];
}
return eventsResultsController_;
}
这是我用来创建 NSFetchRequest
的代码:
- (NSFetchRequest*)makeGetAllFetchRequestForEntity:(NSString*)entityName sortedBy:(NSString*)sortString ascending:(BOOL)ascending {
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
if (sortString != nil) {
// add sorting information
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortString ascending:ascending];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
}
[entity release];
return fetchRequest;
}
上下文对象创建一次,并在应用程序的整个生命周期中以单例形式保存。
我已经检查过,确保当视图控制器被释放时,eventsResultsController_ 被释放。
在堆栈中,我被告知它在 initWithFetchRequest:managementObjectContext:sectionNameKeyPath:cacheName 方法中崩溃了:
#0 0x00f04057 in ___forwarding___
#1 0x00f03f22 in __forwarding_prep_0___
#2 0x00da1b4d in -[NSFetchedResultsController initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:]
#3 0x00008c94 in -[PAHistoryListTableView eventsResultsController] at PAHistoryListTableView.m:125
#4 0x00008a5b in -[PAHistoryListTableView loadData] at PAHistoryListTableView.m:52
#5 0x00008a2a in -[PAHistoryListTableView viewDidLoad] at PAHistoryListTableView.m:43
...
I have an NSFetchedResultsController
inside a subclass of UITableViewController
inside a UINavigationController
. When I run the app, everything works perfectly the first three times I access the view (going to it, then clicking 'Back', then going to it again), but on the fourth (always) it crashes with the following:
-[NSEntityDescription subentitiesByName]: message sent to deallocated instance 0x8b09c80
Any help would be much appreciated.
Here is my getter for the results controller:
- (NSFetchedResultsController*)eventsResultsController {
if (eventsResultsController_ == nil) {
NSFetchRequest *aFetchRequest = [[PADataContext sharedInstance] makeGetAllFetchRequestForEntity:@"PAEvent" sortedBy:@"when" ascending:NO];
// NOTE: crashes on this next line
NSFetchedResultsController *aFetchedResultsContorller = [[NSFetchedResultsController alloc] initWithFetchRequest:aFetchRequest managedObjectContext:[[PADataContext sharedInstance] managedObjectContext] sectionNameKeyPath:@"whenMonth" cacheName:@"AllEvent"];
self.eventsResultsController = aFetchedResultsContorller;
[aFetchedResultsContorller release];
}
return eventsResultsController_;
}
This is the code I use to create my NSFetchRequest
:
- (NSFetchRequest*)makeGetAllFetchRequestForEntity:(NSString*)entityName sortedBy:(NSString*)sortString ascending:(BOOL)ascending {
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
if (sortString != nil) {
// add sorting information
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortString ascending:ascending];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
}
[entity release];
return fetchRequest;
}
The context object is created once and held throughout the lifetime of the app in a singleton.
I have checked the ensure the eventsResultsController_
gets released when the view controller gets dealloc
ed.
In the stack, I'm told it crashed in the initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName
method:
#0 0x00f04057 in ___forwarding___
#1 0x00f03f22 in __forwarding_prep_0___
#2 0x00da1b4d in -[NSFetchedResultsController initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:]
#3 0x00008c94 in -[PAHistoryListTableView eventsResultsController] at PAHistoryListTableView.m:125
#4 0x00008a5b in -[PAHistoryListTableView loadData] at PAHistoryListTableView.m:52
#5 0x00008a2a in -[PAHistoryListTableView viewDidLoad] at PAHistoryListTableView.m:43
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用非保留调用创建了实体:
然后释放了它:
取出版本。当您关注 NSEntityDescription 被过度释放这一事实时,发现这一点是微不足道的。您只在一个地方使用它,因此很容易发现错误。
You created your entity with a non-retaining call:
And then you released it:
Take out the release. This is trivial to find when you focus on the fact that it was the
NSEntityDescription
that was over-released. You only use that in one place, so it's easy to find the mistake.