我正在完成我的核心数据应用程序并开始我的最终测试。
Everyfing工作正常,除了一件事,发生随机,我无法重现它。
这是日志(使用NSZombieEnabled):
2011-07-03 20:27:53.144 MYAPP[1882:707] -[__NSCFType controllerWillChangeContent:]: unrecognized selector sent to instance 0x4a4c490
2011-07-03 20:27:53.149 MYAPP[1882:707] Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[__NSCFType controllerWillChangeContent:]: unrecognized selector sent to instance 0x4a4c490 with userInfo (null)
2011-07-03 20:27:53.165 MYAPP[1882:707] CoreAnimation: ignoring exception: -[__NSCFType controllerWillChangeContent:]: unrecognized selector sent to instance 0x4a4c490
它在这里崩溃了:
NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; // IT'S OK
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Kontrahent" inManagedObjectContext:context]; // IT'S OK
for(NSString *key in kontrahent) [newManagedObject setValue:[kontrahent valueForKey:key] forKey:key]; // IT'S OK
NSError *error = nil;
if (![context save:&error]) { // IT'S NOT OK
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
我的行动层次结构:
1. Open application
2. Open my 'root' list (with NSFetchedResultsController, entity: "Faktura")
3. Tap 'Add' button
4. In my 'Add' view controller I create another object (entity: "Kontrahent")
5. I try to add it to database
6. It crashes / It doesn't.
SCHEME:
+---[moc save:]---> Faktury (my root class)
| ↓
+-----delegate-- FakturaCreator <---[moc save:]--+ <--- HERE IT CRASHES
↓ |
KontrahentCreator ---delegate---+
我知道它与NSFetchedResultsController
和[moc save:]
有关。但我无法找到我的问题,因为它在需要时会崩溃。有时它有效,有时会崩溃。如果你对这个问题有所了解,请帮助我:)
NSFetchedResultsController内容(Faktury.m)
#pragma mark - Fetched results controller
- (NSFetchedResultsController *)fetchedResultsController {
if (__fetchedResultsController != nil) return __fetchedResultsController;
// Setup the table
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Faktura" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Setup the sort descriptors
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"NumerFV" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Create the fetched results controller
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Błąd Krytyczny" message:@"Wystąpił nieznany błąd przy zmienianiu zawartości w bazie danych. Dla dobra twoich danych prosimy niezwłocznie wyjść z aplikacji i spróbować ponownie." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __fetchedResultsController;
}
#pragma mark - Fetched results controller delegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
if([[[controller sections] objectAtIndex:0] numberOfObjects] == 0) {
emptySectionView.hidden = NO;
UIBarButtonItem *editBtn = [[UIBarButtonItem alloc] initWithTitle:@"Edytuj" style:UIBarButtonItemStyleBordered target:self action:@selector(toogleEditing)];
editBtn.enabled = NO;
[self.navigationItem setLeftBarButtonItem:editBtn animated:NO];
[editBtn release];
} else {
emptySectionView.hidden = YES;
self.navigationItem.leftBarButtonItem.enabled = YES;
}
UITableView *table = tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[table insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:(KSTableViewCell *)[table cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[table insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[tableView endUpdates];
}
点击添加按钮(Faktury.m)
- (void)add:(id)sender {
FakturaCreator *form = [[FakturaCreator alloc] init];
form.hidesBottomBarWhenPushed = YES;
form.delegate = self;
form.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:form animated:YES];
[form release];
}
答案 0 :(得分:6)
NSFetchedResultsController
。但是此UIViewController
显示为modalViewController
。我把我的Kontrahent
和你的模态解散并释放了。但NSFRC
的代表仍然有效。
我通过
解决了我的问题self.fetchedResultsController.delegate = nil;
在-dealloc
方法中。
答案 1 :(得分:0)
看起来你正在抓住一个指针,指向一个在你有机会使用之前从内存中删除的对象。确保你保留了任何原本会被自动释放的物品。
从崩溃日志中看起来NSFetchedResultsController委托的指针指向已经释放的内存。
FakturaCreator和KontrahentCreator在保存时是否存在?用他们的dealloc方法捕获应用程序,看看那里的一切是否正常。
答案 2 :(得分:0)
如果您的任何核心数据对象中都有nil值,也会发生这些崩溃。在保存之前查看每个对象是个好主意,以确保所有属性都具有值(即没有任何内容表示nil或“... not nil ...”,这表示对象不符合NSCoding标准) 。如果是这种情况,您可以简单地为属性添加默认值,使它们永远不会为零,然后在转发时分配它们