gpt4 book ai didi

ios - 如何在 cellEdit 之后强制从持久存储中重新加载核心数据

转载 作者:行者123 更新时间:2023-11-28 21:17:01 24 4
gpt4 key购买 nike

我在编辑单元格后无法从核心数据持久存储重新加载数据。

我尝试重新加载 tableView 本身,但这不会强制从持久存储中获取数据,我如何才能强制完全重新加载?

如果我退出 UIViewController 并返回所有工作以便我知道持久存储已正确更新,这是我尝试重新加载但不起作用

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];

NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext;

if([NWTillHelper isDebug] == 1) {
NSLog(@"TransactionListView:tableView:context = %@", context);
}

if (editingStyle == UITableViewCellEditingStyleDelete) {

NSManagedObject *transactionRow = [self.transactionRowsRows objectAtIndex:indexPath.row];

[transactionRow setValue:[NSNumber numberWithInt:[@"999" intValue]] forKey:@"status"];
[transactionRow setValue:[NSNumber numberWithInt:[@"0" intValue]] forKey:@"qty"];
[transactionRow setValue:[NSDecimalNumber decimalNumberWithString:@"0"] forKey:@"price"];
[transactionRow setValue:[NSDecimalNumber decimalNumberWithString:@"0"] forKey:@"discountPercentage"];
[transactionRow setValue:[NSDecimalNumber decimalNumberWithString:@"0"] forKey:@"discountAmount"];
[transactionRow setValue:[NSDecimalNumber decimalNumberWithString:@"0"] forKey:@"amountTax"];
[transactionRow setValue:[NSDecimalNumber decimalNumberWithString:@"0"] forKey:@"sum"];

NSError *error = nil;
if (![context save:&error]) {
if([NWTillHelper isDebug] == 1) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}
}
[self.transactionListViewTbl reloadData];
}
}

最佳答案

只是想提一下。

阅读您的问题后,我觉得它不会使用 Fetch Controller 来修复。 Fetch Controller 非常简洁,它让您无需显式调用 [table reloadData] 即可保持表更新。但是,如果重新加载您的表,数据没有得到更新,则可能有多个托管对象上下文正在更新您的持久存储。

数据已由不同的托管对象上下文更新,您无法在连接表的对象上下文中获得更新。我将从查看我的核心数据堆栈和托管对象上下文的数量开始。如果您需要超过 1 个,您可以订阅更改,或者确保您希望看到的更改是在同一个托管对象上下文中完成的。

希望对您有所帮助。

Managed Object Context - Apple Docs

关于ios - 如何在 cellEdit 之后强制从持久存储中重新加载核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40987813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com