Core Data deleteObject crashing app with EXC_BAD_ACCESS
I'm loading a User object from my fetchResultsController, getting a bunch of related Sites objects from said User, putting them into an array, sorting, then displaying in a 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];
}
Now my scenario is I want to delete this User object and related Sites. However whenever I try and save the delete, my application crashes with a EXC_BAD_ACCESS error. Here is my delete code:
-(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
}
}
I'm not entirely sure why though... I noticed that my mainUser had a retain count of 3 which I assume is because of the sortedMsgs and UITableView... Is it because my UITableView is still showing the data from effectively the User, and then I delete it? Really confused...
Here's the crash log:
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
My only guess at this point is that you may be deleting an object that has a required relationship to another object. Check your relationships within the data model?
Totally a hunch.
精彩评论