简体   繁体   English

无法保存核心数据

[英]Can't save Core Data

I tried saving core data in tableview with NSFetchedResultsController and different sections. 我尝试使用NSFetchedResultsController和其他部分在表格视图中保存核心数据。

When I add data following error appears: 当我添加数据时,出现以下错误:

CoreData: error: Serious application error. CoreData:错误:严重的应用程序错误。 Exception was caught during Core Data change processing. 在核心数据更改处理期间捕获到异常。 This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. 这通常是NSManagedObjectContextObjectsDidChangeNotification的观察者内部的错误。 * -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0] with userInfo (null) *-[__ NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试使用userInfo将对象[0]中的零对象插入(空) Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0] * -[__ NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象中插入零对象[0] *

First throw call stack: (0x181a05900 0x181073f80 0x1818f41a8 0x1818f4040 0x1000a9068 0x1000aa8d8 0x1835905a8 0x183505080 0x183504f48 0x183495108 0x1819aafc4 0x1819aa7e4 0x1819aa564 0x181a0fde4 0x1818eb0f4 0x1822dad2c 0x18349506c 0x1835098d0 0x18349378c 0x183492240 0x1000aa3a0 0x18672fe50 0x1868b34a4 0x18672fe50 0x18672fdcc 0x186717a88 0x186717bd4 0x18672f6e4 0x18672f314 0x186727e30 0x1866f84cc 0x1866f6794 0x1819bcefc 0x1819bc990 0x1819ba690 0x1818e9680 0x182df8088 0x186760d90 0x10004c23c 0x18148a8b8) libc++abi.dylib: terminating with uncaught exception of type NSException` 第一掷调用堆栈:(0x181a05900 0x181073f80 0x1818f41a8 0x1818f4040 0x1000a9068 0x1000aa8d8 0x1835905a8 0x183505080 0x183504f48 0x183495108 0x1819aafc4 0x1819aa7e4 0x1819aa564 0x181a0fde4 0x1818eb0f4 0x1822dad2c 0x18349506c 0x1835098d0 0x18349378c 0x183492240 0x1000aa3a0 0x18672fe50 0x1868b34a4 0x18672fe50 0x18672fdcc 0x186717a88 0x186717bd4 0x18672f6e4 0x18672f314 0x186727e30 0x1866f84cc 0x1866f6794 0x1819bcefc 0x1819bc990 0x1819ba690 0x1818e9680 0x182df8088 0x186760d90 0x10004c23c 0x18148a8b8)的libc ++ abi.dylib:以NSException类型的未捕获异常终止

My code: 我的代码:

MyDetailData* MyDetail = [NSEntityDescription insertNewObjectForEntityForName:@"MyDetailData" inManagedObjectContext:self._privateObjectContext];
[MyDetail setKategorie: NSLocalizedString(@"Test", nil)];
NSError *error = nil;
if (![MyDetail.managedObjectContext save:&error]) {
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}

under iOS6 all was fine with that code 在iOS6下,使用该代码一切都很好

Here is my NSFetchedResultsController 这是我的NSFetchedResultsController

- (NSFetchedResultsController *)fetchedResultsController2 {

    if (fetchedResultsController2 != nil) {
       return fetchedResultsController2;
    }

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription     entityForName:@"MyDetailData" inManagedObjectContext:_privateObjectContext];
    [request setEntity:entity];

    NSSortDescriptor *KategorieDescriptor = [[NSSortDescriptor alloc] initWithKey:@"kategorie" ascending:YES];
    NSSortDescriptor *oderDescriptor = [[NSSortDescriptor alloc] initWithKey:@"order" ascending:YES];
    NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:KategorieDescriptor,oderDescriptor,nameDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];


    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:_privateObjectContext sectionNameKeyPath:@"kategorie" cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController2 = aFetchedResultsController;

    return fetchedResultsController2;

}  

TableView delegates TableView委托

  • numberOfSectionsInTableView numberOfSectionsInTableView
  • numberOfRowsInSection numberOfRowsInSection
  • NSFetchedResultsController didChangeObject -> NSFetchedResultsChangeInsert NSFetchedResultsController didChangeObject-> NSFetchedResultsChangeInsert
  • NSFetchedResultsController didChangeSection -> NSFetchedResultsChangeInsert NSFetchedResultsController didChangeSection-> NSFetchedResultsChangeInsert

called and contain right information - here is relevant code: 并包含权利信息-这是相关代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[fetchedResultsController2 sections] count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController2 sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

    if (userDrivenDataModelChange) return;

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            [self.tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate: {
            [self configureCell:[self.tv cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];

            NSString *sectionKeyPath = [controller sectionNameKeyPath];
            if (sectionKeyPath == nil)
                break;

            NSManagedObject *changedObject = [controller objectAtIndexPath:indexPath];
            NSArray *keyParts = [sectionKeyPath componentsSeparatedByString:@"."];
            id currentKeyValue = [changedObject valueForKeyPath:sectionKeyPath];
            for (int i = 0; i < [keyParts count] - 1; i++) {
                NSString *onePart = [keyParts objectAtIndex:i];
                changedObject = [changedObject valueForKey:onePart];
            }
            sectionKeyPath = [keyParts lastObject];
            NSDictionary *committedValues = [changedObject committedValuesForKeys:nil];

            if ([[committedValues valueForKeyPath:sectionKeyPath] isEqual:currentKeyValue])
                break;

            NSUInteger tableSectionCount = [self.tv numberOfSections];
            NSUInteger frcSectionCount = [[controller sections] count];
            if (tableSectionCount != frcSectionCount) {
                // Need to insert a section
                NSArray *mysections = controller.sections;
                NSInteger newSectionLocation = -1;
                for (id oneSection in mysections) {
                    NSString *sectionName = [oneSection name];
                    if ([currentKeyValue isEqual:sectionName]) {
                        newSectionLocation = [mysections indexOfObject:oneSection];
                        break;
                    }
                }
                if (newSectionLocation == -1)
                    return; // uh oh

                if (!((newSectionLocation == 0) && (tableSectionCount == 1) && ([self.tv numberOfRowsInSection:0] == 0)))
                    [self.tv insertSections:[NSIndexSet indexSetWithIndex:newSectionLocation] withRowAnimation:UITableViewRowAnimationFade];
                NSUInteger indices[2] = {newSectionLocation, 0};
                newIndexPath = [[NSIndexPath alloc] initWithIndexes:indices length:2];
            }


        }

        case NSFetchedResultsChangeMove:
            [self.tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:NO];
            [self.tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                             withRowAnimation:NO];
            break;
        default:
            break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
    switch(type) {

        case NSFetchedResultsChangeInsert:
            if (!((sectionIndex == 0) && ([self.tv numberOfSections] == 1)))
                [self.tv insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            if (!((sectionIndex == 0) && ([self.tv numberOfSections] == 1) ))
                [self.tv deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeMove:
            break;
        case NSFetchedResultsChangeUpdate: 
            break;
        default:
            break;
    }
}

Ideas whats going wrong? 想法出了什么问题?

Problem solved. 问题解决了。 in

didChangeObject

at

 case NSFetchedResultsChangeMove

i changed the line 我换了线

 [self.tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:NO];

  [self.tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                           withRowAnimation:NO];

to

   if (indexPath && newIndexPath) {
                 [self.tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:NO];

                 [self.tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                           withRowAnimation:NO];
  } else {
                 NSLog(@"A table item was moved");
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM