iOS - NSFetchedResultControllerDelegate不更新表

时间:2014-10-01 09:13:14

标签: ios objective-c uitableview magicalrecord nsfetchedresultscontroller

我在生命周期中使用了以下代码的UITableView:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.fetchedResultsController = [UserModel fetchAllSortedBy:@"fullName" ascending:YES withPredicate:nil groupBy:nil delegate:self];
    [self updatePredicate];    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.fetchedResultsController.delegate = nil;
}

和NSFetchedResultControllerDelegate:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    UITableView *tableView = self.tableView;

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeMove:
            break;

        case NSFetchedResultsChangeUpdate:
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}

方法[self updatePredicate]:

- (void) updatePredicate {
    NSPredicate *messageFilter = [NSPredicate predicateWithFormat:@"address != nil"];
    self.fetchedResultsController.fetchRequest.predicate = messageFilter;
    [self.fetchedResultsController performFetch:nil];
    [self.tableView reloadData];
}

为了模拟CoreDate的变化,我添加了一个简单的UIBarButton事件:

- (IBAction)didChangeSomething:(id)sender {
    [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
        UserDAO *userDAO = [[UserDAO alloc] initWithContext:localContext];
        UserModel *userDB = [userDAO getUserById:@"a3535ee6-645a-4f6e-9970-4df65120353e"];
        userDB.address = userDB.address == nil ? @"Testing" : nil;
        [localContext saveToPersistentStoreAndWait];
    }]; 
}

其他一些信息:

  • UserModel是CoreData Entity
  • UserDAO是我的类,它调用此方法

    返回[UserModel findFirstByAttribute:@“uid”withValue:@“a3535ee6-645a-4f6e-9970-4df65120353e”inContext:self.context];

我有这种奇怪的行为:

  1. 如果我第一次打开我的UITableViewController(当地址字段为nil时)没有显示任何行,那么我点击我的UIBarButton并没有任何反应。 (但在我的Sqlite文件中,我可以在“测试”中看到地址的真实变化。)。
  2. 如果我关闭我的UITableViewController并重新打开它,我可以看到TableView上的条目。现在,如果我点击UIBarButton,表格会正确更新,可以多次添加和删除条目。
  3. 为什么我有这种行为?发生了什么?当我更改字段“地址”的值时,为什么没有更新TableView?

    编辑1: 如果我改变:

    UserDAO *userDAO = [[UserDAO alloc] initWithContext:localContext];
    

    UserDAO *userDAO = [[UserDAO alloc] initWithContext:[NSManagedObjectContext contextForCurrentThread]];
    

    一切正常。

0 个答案:

没有答案