我在生命周期中使用了以下代码的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];
}];
}
其他一些信息:
UserDAO是我的类,它调用此方法
返回[UserModel findFirstByAttribute:@“uid”withValue:@“a3535ee6-645a-4f6e-9970-4df65120353e”inContext:self.context];
我有这种奇怪的行为:
为什么我有这种行为?发生了什么?当我更改字段“地址”的值时,为什么没有更新TableView?
编辑1: 如果我改变:
UserDAO *userDAO = [[UserDAO alloc] initWithContext:localContext];
与
UserDAO *userDAO = [[UserDAO alloc] initWithContext:[NSManagedObjectContext contextForCurrentThread]];
一切正常。