如何仅显示某些记录时如何编辑nsfetchresults记录?

时间:2013-03-23 05:20:09

标签: ios edit nsfetchrequest uitableview

我正在尝试从另一个viewcontroller编辑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)后必须包含在现有部分中的行   等于之前该部分中包含的行数   update(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 Controller

- (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;

}

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

由于

3 个答案:

答案 0 :(得分:0)

你是如何“仅显示一些记录”的?使用NSPredicate过滤结果或其他任何内容?

我认为您需要找到问题,是因为您从另一个视图控制器操作一个tableview?

答案 1 :(得分: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;
}

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

答案 2 :(得分:0)

找到了答案!

[self.tableView updateConstraintsIfNeeded];

需要将此行添加到

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller