ios - 更改的核心数据未显示在 TableView 中

标签 ios objective-c core-data

我已经调试这个问题几个小时了,需要一些帮助。首先,下面是我遇到问题的功能代码:

- (void)addAllHomeworkInstances:(Homework *)homework
{
    // Gets all the subjects that match
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Subject"];
    request.predicate = [NSPredicate predicateWithFormat:@"(name = %@) AND (teacher = %@)", self.subject.name, self.subject.teacher];

    // Finds matches from request
    NSError *error;
    NSArray *matches = [[homework managedObjectContext] executeFetchRequest:request error:&error];
    if (error || !matches) {
        NSLog(@"Error when trying to add homework for all subjects: %@", error);
    }
    else {
        [matches enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            // Adds the homework to each subject
            if ([obj isKindOfClass:[Subject class]]) {
                Subject *eachSubject = (Subject *)obj;

                // Makes sure subject is not the current one since
                if (![eachSubject isEqual:self.subject]) {
                    NSMutableSet *homeworkInSubject = [[NSMutableSet alloc] initWithSet:eachSubject.homework];
                    [homeworkInSubject addObject:homework];
                    eachSubject.homework = homeworkInSubject;
                }
            }
        }];
    }
}

现在问题出现在 eachSubject.homework = homeworkInSubject; 行上。基本上,我的 View Controller 显示给定类(class)的作业,当我们为类(class)添加作业时会调用此函数。

如果我注释掉上面的行,添加的作业将显示在当前 View Controller 上,因为它已被添加到类中。注释行应该做的是通过向其添加作业来更新其他科目(不是当前科目)的作业集。当我取消注释行时,其他主题在核心数据中得到更新。

问题在于,即使当前主题的核心数据得到更新,当前 View Controller 也不会显示主题。我尝试执行 performFetch 来更新数据,为 tableview 执行 reloadData,甚至重新初始化 fetchedResultsController 但没有任何效果。

我认为更改核心数据是问题的原因,但我通过简单地在 if 条件中使用以下代码进行了测试,证明情况并非如此:

if (![eachSubject isEqual:self.subject]) {
    NSMutableSet *homeworkInSubject = [[NSMutableSet alloc] initWithSet:eachSubject.homework];
    eachSubject.homework = homeworkInSubject;
}

所以现在,我完全无助。我尝试了很多东西,但没有一个给我正确的结果。

编辑:

这是我用来通过初始化 fetchResultsController 显示 tableview 的代码,但我 99% 确定这没有问题,因为评论上面有问题的行仍然可以正常工作:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initializeFetchResultsController];
}

// Initializes the NSFetchedResultsController to the following request
- (void)initializeFetchResultsController
{
    // Makes a request for the given entity
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Homework"];
    request.predicate = [NSPredicate predicateWithFormat:@"inClass = %@", self.subject];
    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"dueDate"
                                                              ascending:NO],
                                [NSSortDescriptor sortDescriptorWithKey:@"title"
                                                              ascending:YES
                                                               selector:@selector(localizedStandardCompare:)]];
    // Creates a fetchedResultsController
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                        managedObjectContext:[self.subject managedObjectContext]
                                                                          sectionNameKeyPath:nil
                                                                                   cacheName:nil];
}

最佳答案

你什么时候告诉 TableView 重新加载数据?获取、枚举和插入的顺序不是连续的(您使用的是 block ),因此它们发生在各种不同的时间。我假设你正在做类似的事情:

[self addAllHomeWorkInstances:homework];
[self.tableView reloadData];

这可能行不通,因为您不能保证完成更新 Subject 对象。您可以尝试的一件事是在延迟后重新加载数据,看看是否是问题所在。更好的方法是添加一个完成 block ,这样你就有了类似的东西

[self addAllHomeworkInstances:homework completion: ^{
    [self.tableView reloadData];
}];

每次在 enumerateObjectsUsingBlock block 内调用完成 block 。

弄清楚后编辑:

问题似乎是您的对象之间的关系。 homework 和 subject 必须是多对多的关系,而 homework 在发布问题时只属于一个 subject。所以当其他科目接手作业时,它从当前科目中删除。

关于ios - 更改的核心数据未显示在 TableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25390781/

相关文章:

ios - 设置 CVCalendar

ios - 自定义 UIButton 在 UIImageView 中不起作用

iphone - NSPredicate - 未按预期工作

objective-c - 核心数据,NSArrayContainer 多个数组的Arraycontent

ios - Xcode:存档在验证期间有不正确的包 ID 和配置文件

c# - 旋转时 UIPageViewController 的 NSInternalInconsistencyException

ios - 新初始化和数组初始化之间的区别

iphone - 在 UICollectionView 中设置边框

ios - 有什么方法可以限制核心数据中的重复条目吗?

ios - 如何使用 RxSwift 强制点击 UIButton?