核心数据:执行提取时出现错误访问错误
在 UITableView 的提交中,我使用以下代码删除一个对象:
[_context deleteObject:[_StudiessList objectAtIndex:indexPath.row]];
NSError *error;
if (![_context save:&error]) {
// Handle error
NSLog(@"Unresolved error series %@, %@", error, [error userInfo]);
}
[self LoadData]; // bad access fire here
其中 LoadData
是重新填充表视图的函数
它的代码是:
iPaxeraAppDelegate *appDelegate = (iPaxeraAppDelegate *)[[UIApplication sharedApplication] delegate];
self.context = appDelegate.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Studies" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
NSArray *sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"StudyDate" ascending:YES] autorelease]];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error;
@try {
// error done here exactly at fetch
self.StudiessList = [_context executeFetchRequest:fetchRequest error:&error];
}
@catch (NSException * e) {
NSLog(e);
}
[fetchRequest release];
[StudiessList retain];
LoadData
在 executeFetchRequest 上崩溃:
In the commit of UITableView I delete an object using the following code:
[_context deleteObject:[_StudiessList objectAtIndex:indexPath.row]];
NSError *error;
if (![_context save:&error]) {
// Handle error
NSLog(@"Unresolved error series %@, %@", error, [error userInfo]);
}
[self LoadData]; // bad access fire here
where LoadData
is the function to refill the table view
Its code is:
iPaxeraAppDelegate *appDelegate = (iPaxeraAppDelegate *)[[UIApplication sharedApplication] delegate];
self.context = appDelegate.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Studies" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
NSArray *sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"StudyDate" ascending:YES] autorelease]];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error;
@try {
// error done here exactly at fetch
self.StudiessList = [_context executeFetchRequest:fetchRequest error:&error];
}
@catch (NSException * e) {
NSLog(e);
}
[fetchRequest release];
[StudiessList retain];
LoadData
crashes upon executeFetchRequest:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题可能就在这里:
通过附加显式自动释放,您设置了一种情况,在这种情况下,排序描述符可能会在仍然需要时被释放。当 fetch 去获取排序描述符时,它会崩溃,因为该对象不存在。
显式自动释放可能会出现问题,因为它们与定义它们的直接作用域的内存池的耗尽密切相关。尽管许多人随意使用自动释放,但您实际上应该只在从方法返回值时使用它,因为自动释放告诉运行时,“我已经完全完成了这个对象,我不关心它会发生什么。”如果你还没有完成这个对象,那么你就会遇到问题。
使用便捷方法中的隐式自动释放可以消除此问题,因为自动释放是在另一个范围(例如持有便利方法的类对象)中发出的。然后它进入本地作用域并可以保留,而不必担心本地作用域中的自动释放会杀死它。
尝试:
...看看是否可以解决崩溃问题。
Your problem is probably here:
By attaching an explicit autorelease you set up a circumstance in which the sort descriptor may be deallocated while it is still needed. When the fetch goes to grab the sort descriptor, it crashes because the object is not there.
Explicit autoreleases can be problematical because they are intimately linked to the draining of the memory pool of the immediate scope they are defined in. Although many people use autorelease casually, you should really only use it when you are returning a value from a method because autorelease tells, the runtime, "I am completely done with this object and I don't care what happens to it." If you are not done with the object, then you will have problems.
Using an implicit autorelease from a convenience method eliminates this problem because the autorelease is issued in another scope e.g. the class object holding the convenience method. It then enters the local scope and can be retained without having to worry about an autorelease in the local scope killing it.
Try:
... and see if that resolves the crash.