我正在从我的fetchResultsController加载一个User对象,从所述User获取一堆相关的Sites对象,将它们放入一个数组,排序,然后在UITableView中显示。
-(void)loadMainUser {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0];
if([sectionInfo numberOfObjects]) {
NSUInteger indexArr[] = {0,0};
NSIndexPath *indexSet = [NSIndexPath indexPathWithIndexes:indexArr length:2];
mainUser = (User *)[self.fetchedResultsController objectAtIndexPath:indexSet];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"siteName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
sortedMsgs = [[mainUser.sitesToUser allObjects] mutableCopy];
[sortedMsgs sortUsingDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];
}
[self.theTableView reloadData];
}
现在我的方案是我要删除此User对象和相关站点。但是,每当我尝试保存删除时,我的应用程序都会因EXC_BAD_ACCESS错误而崩溃。这是我的删除代码:
-(void)deleteUser {
sortedMsgs = nil;
mainUser = nil;
NSUInteger indexArr[] = {0,0};
NSIndexPath *indexSet = [NSIndexPath indexPathWithIndexes:indexArr length:2];
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexSet]];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
} else {
// do something
}
}
我不完全确定为什么虽然......我注意到我的mainUser的保留计数为3,我假设是因为sortedMsgs和UITableView ...是因为我的UITableView仍然有效地显示数据用户,然后我删除它?真的很困惑......
这是崩溃日志:
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000000000016
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Application Specific Information:
objc_msgSend() selector name: isTemporaryID
iPhone Simulator 235, iPhone OS 4.2 (iPhone/8C134)
Thread 0 Crashed: Dispatch queue: com.apple.main-thread
0 libobjc.A.dylib 0x01613a63 objc_msgSend + 23
1 CoreData 0x00e20d26 -[NSManagedObjectContext save:] + 566
2 Clicky 0x00002cfd -[RootViewController deleteUser] + 358 (RootViewController.m:71)
3 UIKit 0x003eaa6e -[UIApplication sendAction:to:from:forEvent:] + 119
4 UIKit 0x005f8167 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156
5 UIKit 0x003eaa6e -[UIApplication sendAction:to:from:forEvent:] + 119
6 UIKit 0x004791b5 -[UIControl sendAction:to:forEvent:] + 67
答案 0 :(得分:1)
我唯一想到的是你可能正在删除与另一个对象有必需关系的对象。检查数据模型中的关系?
完全是一种预感。