我对核心数据很陌生。
我的数据模型有User
一个实体,该实体具有fullName
从服务器获取的属性。我正在Users
使用NSFetchedResultsController
. 随着所有的Users
属性fullName
更新,我希望 MOC 发送一个确实更改通知。但是,它没有这样做。因此,我的 FRC 也没有更新。
我已经清理、构建、修改了我的数据模型,并构建了甚至删除User.h/m
并重新生成了它。我仍然无法弄清楚问题所在。fullName
实际上正在更新,我可以手动重新加载表格视图并查看更改。我的问题是什么?
代码:
设置fullName
:
- (BOOL)methodName:(NSDictionary *)data
{
self.fullName = data[@"fullName"];
self.imageData = data[@"image"];
}
表格视图:
- (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 委托方法:
- (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;
}