0

我正在尝试从另一个视图控制器编辑 nsfetchresults 记录,如果显示我的所有数据库条目,它工作正常。但是当我按日期过滤表格并尝试编辑它时,我收到了这个错误

2013-03-22 23:09:02.895 easyDB[1079:907] * -[UITableView _endCellAnimationsWithContext:] 中的断言失败,/SourceCache/UIKit/UIKit-2380.17/UITableView.m:1070 2013-03-22 23:09: 18.865 easyDB[1079:907] 捕获异常!

NSInternalInconsistencyException

无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (6) 必须等于更新前该节中包含的行数 (2),加上或减去数字从该部分插入或删除的行数(0 插入,0 删除)加上或减去移入或移出该部分的行数(0 移入,0 移出)。

我发送

    NSIndexPath *indexpath = (NSIndexPath*)sender;
            GlucoseDetailViewController *glucoseDetailsVC = (GlucoseDetailViewController*)segue.destinationViewController;
            GlucoseRecords *rec = [self.fetchedResultsController objectAtIndexPath:indexpath];
[glucoseDetailsVC setSentRecord:rec];

在编辑视图中

    [self.sentRecord setGlucose:[NSNumber numberWithInt:[[txtGlucoseReading text] intValue]]];

    [self.sentRecord setDate:self.dateToAdd];
    [self.sentRecord setMealType:[NSString stringWithString:self.strMealOption]];
    [self.sentRecord setQuickNote:[NSString stringWithString:self.strQuickNote]];
    [self.sentRecord setDeleted:[NSNumber numberWithBool:NO]];
    [self.sentRecord setWhoRecorded:[self.settings currentUser]];

    NSError *error;
    if (![[self.settings managedObjectContext] save:&error])
        NSLog(@"Failed to add default user with error: %@", [error domain]);

FetchedResults 控制器

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil && [self.strCurrentEntetyName isEqualToString:self.strEntetyName] &&(self.filteChanged==[NSNumber numberWithBool:NO])) {
        return _fetchedResultsController;
    }


    self.filteChanged = [NSNumber numberWithBool:NO];
    self.strCurrentEntetyName = [NSString stringWithString:self.strEntetyName];

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[self strEntetyName]];
    //NSEntityDescription *entity = [NSEntityDescription
    //                               entityForName:@"Records" inManagedObjectContext:[self.settings managedObjectContext]];

    if (self.isDateFilterSet==[NSNumber numberWithBool:NO])
    {

    fetchRequest.predicate = [NSPredicate predicateWithFormat:@" whoRecorded.username = %@ AND deleted=0", [self.settings.currentUser username]];
    }
    else
    {
        fetchRequest.predicate = [NSPredicate predicateWithFormat:@" (whoRecorded.username = %@) AND (deleted = 0) AND (date =>%@) AND (date<=%@)", [self.settings.currentUser username],self.dateMinSetFilter ,self.dateMaxSetFilter];


    }

    NSLog(@"User:%@",[self.settings.currentUser username]);

   // [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                              initWithKey:@"date" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:100];

    NSFetchedResultsController *theFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:[self.settings managedObjectContext] sectionNameKeyPath:nil
                                                   cacheName:nil];
    self.fetchedResultsController = theFetchedResultsController;




    _fetchedResultsController.delegate = self;




    return _fetchedResultsController;

}

我了解问题所在,但我看不到任何解决问题的好方法。我可以在编辑之前重置表格以显示所有条目,但我认为这不是一个好的解决方案。有没有更好的方法来解决这个问题?

谢谢

4

3 回答 3

0

你是如何“只显示一些记录”的?使用 NSPredicate 过滤结果或其他什么?

我认为您需要定位问题,是因为您从另一个视图控制器操作一个表视图吗?

于 2013-03-23T05:29:46.617 回答
0

试试这个代码:

在调用解析方法之前请写:self.fetchedResultsController = nil;

- (NSFetchedResultsController *)fetchedResultsController // attaches an NSFetchRequest to this UITableViewController
{
    if (_fetchedResultsController != nil )
    {
        return _fetchedResultsController;
    }

    NSLog(@"Length:: %d",[[_fetchedResultsController mutableCopy] length]);

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"YourEntityName"];
    [fetchRequest setIncludesSubentities:YES];

    // Sort by Application name
    NSSortDescriptor* sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
    //Sort by CommandNameloca
    NSArray* sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor1, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    [fetchRequest setFetchBatchSize:100];

    tempArr=[[NSMutableArray alloc]initWithArray:sortDescriptors];

    NSLog(@"****----- tempArr Count == %d",[tempArr count]);

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"date" cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;

    if (![self.fetchedResultsController performFetch:&error])
    {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    return _fetchedResultsController;
}

请在日志中查看数组计数。它将显示您获得的总记录。请分享你的结果。好运.. !!

于 2013-03-23T06:47:35.553 回答
0

找到了答案!

[self.tableView updateConstraintsIfNeeded];

需要将此行添加到

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
于 2013-03-23T20:36:29.370 回答