在提交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:
答案 0 :(得分:0)
你的问题可能就在这里:
NSArray *sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"StudyDate" ascending:YES] autorelease]];
通过附加显式自动释放,您可以设置一种情况,在该情况下,排序描述符可能会在需要时被释放。当fetch去抓取排序描述符时,它崩溃了,因为对象不在那里。
显式自动释放可能有问题,因为它们与它们所定义的直接范围的内存池的消耗密切相关。虽然许多人随便使用自动释放,但是当你从一个值返回一个值时,你应该只使用它方法,因为autorelease告诉运行时,“我完全完成了这个对象,我不在乎它会发生什么。”如果您没有完成该对象,那么您将遇到问题。
使用方便方法的隐式自动释放消除了这个问题,因为自动释放是在另一个范围内发布的,例如持有方便方法的类对象。然后它进入本地范围并且可以保留而不必担心在本地范围内的自动释放导致它被杀死。
尝试:
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"StudyDate " ascending:YES]];
...看看是否可以解决崩溃问题。