我正在构建一个预算应用程序并遇到一些核心数据问题。我有一个表结构,其中有主要类别和子类别。每个人都有一个名字,每个组合只有一个名字有效。(即我可以在几个主要类别中购买子类别保险,但不能在一个主要类别中购买两次保险)。所以我可以毫无问题地添加、删除和重命名主要类别和子类别。但是,当我重命名一个主类别并再次打开它以查看其子类别时(这是在导航控制器中,您选择一个主类别,然后在下一个视图中您会看到子类别),暂时没有可见。几秒钟后,通过重新打开子类别进行“重新加载”,它们将再次可见。
我正在使用 FetchedResultsController。知道我在哪里遗漏了什么吗?我需要在某处重新加载吗?但我认为我不必在使用 FetchedResultsController 时重新加载表格?
这些是相关的代码部分:
#pragma mark Initializer and view setup
- (void)setupFetchedResultsController
{
NSError *error = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"SpendingCategory"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"position" ascending:YES]];
[self.mainCategory.managedObjectContext executeFetchRequest:request error:&error];
request.predicate = [NSPredicate predicateWithFormat:@"belongsToMainCategory.name = %@", self.mainCategory.name];
self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:request
managedObjectContext:self.mainCategory.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
}
- (void)viewDidLoad
{
//Edit/Done button
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(editTable:)];
[self.navigationItem setRightBarButtonItem:editButton];
[super viewDidLoad];
self.sumTitleLabel.text = [NSString stringWithFormat:@"%@%@", NSLocalizedString(@"Budget/Month", nil), @":"];
self.categorySumTitleLabel.text = [NSString stringWithFormat:@"%@%@", self.mainCategory.name, @":"];
[self refreshIcon];
self.iconButton.hidden = YES;
self.iconImage.hidden = NO;
self.mainCategoryNameTextField.text = self.mainCategory.name;
self.mainCategoryNameTextField.delegate = self;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.sumLabel.text = [[DatabaseFetches budgetPerMonthForManagedObjectContext:self.mainCategory.managedObjectContext] getLocalizedCurrencyString];
self.categorySumLabel.text = [[self.mainCategory getMonthlyCostsOfAllSpendingCategories] getLocalizedCurrencyString];
[self refreshIcon];
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}