gpt4 book ai didi

ios - NSManagedObjectContext 看不到实体属性的更改

转载 作者:行者123 更新时间:2023-11-29 13:02:08 33 4
gpt4 key购买 nike

我是 Core Data 的新手。

我的数据模型有一个 User 实体,它有一个属性 fullName,它是从服务器获取的。我使用 NSFetchedResultsController 在 TableView 中显示所有 Users 的列表。当所有 Users 都更新了他们的 fullName 属性时,我希望 MOC 发送一个 did change 通知。然而,它并没有这样做。因此,我的 FRC 也没有更新。

我已经清理并构建、修改了我的数据模型并构建甚至删除了 User.h/m 并重新生成了它。我仍然无法弄清楚问题是什么。 fullName 实际上正在更新,我可以手动重新加载 TableView 并查看更改。我的问题是什么?

代码:

设置全名:

- (BOOL)methodName:(NSDictionary *)data
{
self.fullName = data[@"fullName"];
self.imageData = data[@"image"];
}

表格 View :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.frc.sections[section] numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];

return cell;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
User *user = [self.frc objectAtIndexPath:indexPath];

cell.imageView.image = [UIImage imageWithData:user.imageData];
cell.textLabel.text = user.fullName;
}

设置 FRC:

- (void)viewDidLoad
{
[super viewDidLoad];

if (!self.model) self.model = [XPModel sharedInstance];
[self.model addDelegate:self];

if (!self.frc) {
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([User class])];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"fullName" ascending:YES]];
request.fetchBatchSize = 10;

self.frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.model.moc sectionNameKeyPath:nil cacheName:nil];
self.frc.delegate = self;
}

NSError *error;
[self.frc performFetch:&error];

NSAssert(!error, error.localizedDescription);
}

FRC 委托(delegate)方法:

- (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]; // Only this is called, and only at the beginning
break;

case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;

case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; // Never called
break;

case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray
arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}

设置模型:

- (void)coreDataSetup
{
NSError *error;

NSURL *storeURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].lastObject URLByAppendingPathComponent:@"userdb.sqlite"];;
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];

NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
NSAssert([storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error], error.localizedDescription)

self.moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
self.moc.persistentStoreCoordinator = storeCoordinator;
}

最佳答案

来自这个委托(delegate)回调的文档:

This method may be invoked many times during an update event (for example, if you are importing data on a background thread and adding them to the context in a batch). You should consider carefully whether you want to update the table view on receipt of each message.

我建议您不要依赖此机制。最好在导入期间禁用委托(delegate)。完成后只需保存并重新加载您的表格(或根据一些批处理数量定期)。

我没有看到你在任何地方保存 - 你也应该在导入大量数据时定期这样做。

关于ios - NSManagedObjectContext 看不到实体属性的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19522674/

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