1
我從我的fetchResultsController加載一個用戶對象,從所述用戶那裏得到一堆相關的Sites對象,將它們放入一個數組,排序,然後在UITableView中顯示。核心數據deleteObject與EXC_BAD_ACCESS崩潰的應用
-(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];
}
現在我的場景是我想刪除此用戶對象和相關網站。但是,每當我嘗試並保存刪除,我的應用程序崩潰與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