【发布时间】:2011-03-31 12:13:11
【问题描述】:
在 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: 上崩溃
【问题讨论】:
-
你也在泄露
StudiesList。感觉它可能是一个保留属性,对self.StudiessList的调用已经保留了返回的数组。向它发送另一个保留将导致它泄漏,如果它与在 dealloc 中发送的释放之外的另一个释放不平衡。 -
如果您遵循样式约定,您的代码将更易于阅读。只有框架名称、类名称和常量应该以大写字母开头。 (另外,StudyssList 拼写错误)。所以
StudiessList应该是studiesList和LoadData应该是loadData。遵循约定将使您的代码更容易被其他人阅读。苹果有一个风格指南埋在某个地方。您应该尝试追踪并关注它。
标签: objective-c uitableview core-data nsfetchrequest