iOS-NSFetchedResultControllerDelegate不更新表
我有一个UITableView,在生命周期中包含以下代码:iOS-NSFetchedResultControllerDelegate不更新表,ios,objective-c,uitableview,magicalrecord,nsfetchedresultscontroller,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)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];
}
为了模拟对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实体
- UserDAO是调用此方法的my类 返回[文本:self.context中的UserModel findFirstByAttribute:@“uid”及其值:@“a3535ee6-645a-4f6e-9970-4df65120353e”)
UserDAO *userDAO = [[UserDAO alloc] initWithContext:localContext];
与
一切正常。什么是
UserDAO
,UserModel
,userDB.address
?请清除代码(删除不相关的代码行)。要创建NSManagedObject
对象,请尝试MR_createEntity
方法我已更新了问题。我总是使用MR_createEntity方法创建带有MagicalRecord的实体。我不明白为什么在第一次地址为零时,如果我更改了这个值,什么也不会发生(用click UIBarButton更新)。第二次,地址为!=nil,字段的更改工作正常(表正确)。请确保您在rightNSManagedObjectContext
中创建了新对象。试试这个:replaceuserDB.address=userDB.address==nil@“测试”:无代码>通过userDB.address=@“测试”代码>我试过了,但没有任何改变。这是ManagedObjectContext的问题,但我无法修复它。。。
UserDAO *userDAO = [[UserDAO alloc] initWithContext:localContext];
UserDAO *userDAO = [[UserDAO alloc] initWithContext:[NSManagedObjectContext contextForCurrentThread]];