Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS-NSFetchedResultControllerDelegate不更新表_Ios_Objective C_Uitableview_Magicalrecord_Nsfetchedresultscontroller - Fatal编程技术网

iOS-NSFetchedResultControllerDelegate不更新表

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]; } -

我有一个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];
}
为了模拟对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”)

我有一种奇怪的行为:

  • 如果我第一次打开UITableViewController(当地址字段为nil时),没有显示任何行,那么我单击UIBarButton,什么也没有发生。(但在我的Sqlite文件中,我可以在“测试”中看到地址的真正变化)
  • 如果我关闭UITableViewController并重新打开它,我可以在TableView上看到该条目。现在,如果单击UIBarButton,表将正确更新,添加和删除条目的次数将增加
  • 我为什么有这种行为?发生了什么事?为什么更改字段“Address”的值时TableView没有更新

    编辑1: 如果我改变:

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


    一切正常。

    什么是
    UserDAO
    UserModel
    userDB.address
    ?请清除代码(删除不相关的代码行)。要创建
    NSManagedObject
    对象,请尝试
    MR_createEntity
    方法我已更新了问题。我总是使用MR_createEntity方法创建带有MagicalRecord的实体。我不明白为什么在第一次地址为零时,如果我更改了这个值,什么也不会发生(用click UIBarButton更新)。第二次,地址为!=nil,字段的更改工作正常(表正确)。请确保您在right
    NSManagedObjectContext
    中创建了新对象。试试这个:replace
    userDB.address=userDB.address==nil@“测试”:无通过
    userDB.address=@“测试”我试过了,但没有任何改变。这是ManagedObjectContext的问题,但我无法修复它。。。
    
    UserDAO *userDAO = [[UserDAO alloc] initWithContext:localContext];
    
    UserDAO *userDAO = [[UserDAO alloc] initWithContext:[NSManagedObjectContext contextForCurrentThread]];