核心数据:执行提取时出现错误访问错误

发布于 2024-10-28 08:24:57 字数 1301 浏览 4 评论 0原文

在 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];

LoadDataexecuteFetchRequest 上崩溃:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋凉 2024-11-04 08:24:57

您的问题可能就在这里:

NSArray *sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"StudyDate" ascending:YES] autorelease]]; 

通过附加显式自动释放,您设置了一种情况,在这种情况下,排序描述符可能会在仍然需要时被释放。当 fetch 去获取排序描述符时,它会崩溃,因为该对象不存在。

显式自动释放可能会出现问题,因为它们与定义它们的直接作用域的内存池的耗尽密切相关。尽管许多人随意使用自动释放,但您实际上应该只在从方法返回值时使用它,因为自动释放告诉运行时,“我已经完全完成了这个对象,我不关心它会发生什么。”如果你还没有完成这个对象,那么你就会遇到问题。

使用便捷方法中的隐式自动释放可以消除此问题,因为自动释放是在另一个范围(例如持有便利方法的类对象)中发出的。然后它进入本地作用域并可以保留,而不必担心本地作用域中的自动释放会杀死它。

尝试:

NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"StudyDate " ascending:YES]];

...看看是否可以解决崩溃问题。

Your problem is probably here:

NSArray *sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"StudyDate" ascending:YES] autorelease]]; 

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:

NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"StudyDate " ascending:YES]];

... and see if that resolves the crash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文